When would you use delegates in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, delegates are type-safe references to methods. You use them when behavior needs to be passed around as data, especially for callbacks, event notification, small strategy selection, and APIs that should accept caller-supplied logic without coupling to a concrete class.
Use delegates for callbacks
A delegate is a natural fit when one component needs to call user-provided code later. This keeps the component generic while letting the caller decide what should happen.
This pattern is simpler than creating an interface when the only extension point is one method.
Use delegates for events and notifications
C# events are built on delegates. If an object needs to announce that something happened without knowing who is listening, delegates are the mechanism underneath.
Events are the right choice when multiple subscribers may react independently. The publisher stays decoupled from subscriber implementations.
Use delegates for lightweight strategy injection
If a method needs one small piece of caller-defined behavior, a delegate is often better than a whole interface hierarchy.
This is exactly why the framework includes Action, Func, and Predicate. They cover the most common delegate shapes without making you define a custom delegate type every time.
Use custom delegates when the signature carries meaning
Although Func and Action are convenient, a named delegate can make an API easier to read when the parameter represents a domain concept.
A named delegate documents intent more clearly than Func<decimal, bool> when the rule has business meaning.
When not to use delegates
Delegates are not a replacement for every abstraction. If behavior requires stateful collaboration, multiple related methods, or versioned contracts, an interface or class is usually better. Delegates shine when the extension point is narrow and behavior-focused.
For example, a payment provider abstraction probably deserves an interface with explicit methods and dependency injection. A "sort comparison" or "retry callback" usually fits a delegate perfectly.
Lambda expressions make delegates practical
Delegates became much more useful once lambdas made them concise to create. LINQ depends heavily on this model.
The Where and Select calls accept delegates, which is why callers can express logic inline without subclassing anything.
Common Pitfalls
- Defining a custom delegate when
Action,Func, orPredicatealready expresses the signature clearly. - Using delegates for large, stateful collaborations that should be modeled with interfaces or services.
- Forgetting that multicast delegates call all subscribed methods, which can affect ordering and error handling.
- Exposing raw delegates where an
eventwould be safer for public notification APIs. - Hiding business meaning behind generic delegate types when a named delegate would improve readability.
Summary
- Use delegates when code needs to accept behavior as an argument.
- They are especially useful for callbacks, events, and small strategy hooks.
- Prefer
Action,Func, andPredicatefor common signatures. - Use named delegates when the method shape has domain meaning.
- Switch to interfaces when the abstraction needs multiple operations or persistent state.
Related reading
- When you await on async call--is it really asynchronous programming in C
- WhenAll on the large number of Task
- Where and how is the _ViewStart.cshtml layout file linked?
- Where are the Properties.Settings.Default stored?
- Where can I find a NuGet package for upgrading to System.Web.Http v5.0.0.0?
- Where can I put custom classes in ASP.NET MVC?
- Where does error CS0433 Type 'X' already exists in both A.dll and B.dll come from?
- where is gacutil.exe?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.