Delegate in C# Code: Types, Examples, and Best Practices
Introduction
Delegate in C# is a strong feature that lets you use methods like objects. This means you can pass methods as arguments, store them in variables, and call them at different times. Using delegates makes your programs more flexible, easier to test, and simpler to manage.
What is a Delegate in C#?
A delegate in C# is like a safe reference to a method. It checks that the method has the same return type and parameters as the delegate. This make your code safer and more reliable. Unlike old function pointers in C or C++, delegates are secure, object-based, and work well with the .NET framework.
Why Use Delegate in C#?
Delegates make your code more reusable and help separate different parts of your program. You can use them for callbacks, handling events, or passing methods to other methods. Because of this flexibility, many advanced C# features, such as events, LINQ, and async programming, depend on delegates.
Types of delegate in C#
1. Single-cast Delegate
- Refers to a single method at a time.
- When you invoke it, only one method executes.
public delegate void MyDelegate(string msg);
class Program
{
static void Print(string msg) =>
Console.WriteLine(msg);
static void Main()
{
MyDelegate del = Print; // single method assigned
del("Hello from Single-cast Delegate");
}
}
2. Multi-cast Delegate
- Refers to multiple methods in its invocation list.
- Methods are called in order they were added.
- Uses
+or+=operator to add methods, and-=to remove.
public delegate void MyDelegate(string msg);
class Program
{
static void Print1(string msg) =>
Console.WriteLine("1: " + msg);
static void Print2(string msg) =>
Console.WriteLine("2: " + msg);
static void Main()
{
MyDelegate del = Print1;
del += Print2; // multicast delegate
del("Hello from Multi-cast Delegate");
}
}
3. Generic Delegates
C# provides built-in generic delegates to avoid writing custom delegate types:
- Func → Encapsulates a method that returns a value.
- Can take 0–16 input parameters, but must have a return type.
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(5, 3)); // 8
- Action → Encapsulates a method that does not return a value (
void).
Action<string> greet = name =>
Console.WriteLine("Hello " + name);
greet("John");
- Predicate → Encapsulates a method that returns a bool and takes one parameter.
Predicate<int> isEven = x => x % 2 == 0;
Console.WriteLine(isEven(4)); // True
4. Anonymous Methods with Delegates
- Introduced in C# 2.0.
- Allows declaring methods inline without a separate method definition.
MyDelegate del = delegate(string msg)
{
Console.WriteLine("Anonymous: " + msg);
};
del("Hi there");
5. Lambda Expressions with Delegates
- Introduced in C# 3.0.
- Shorter syntax than anonymous methods.
MyDelegate del = msg =>
Console.WriteLine("Lambda: " + msg);
del("Hello");
Summary in form of table
| Type | Return Type | Parameters | Example |
|---|---|---|---|
| Single-cast | Any | Defined by delegate | Holds one method |
| Multi-cast | Any | Defined by delegate | Holds multiple methods |
| Func | Non-void | 0–16 | Func<int,int,int> |
| Action | Void | 0–16 | Action<string> |
| Predicate | bool | 1 | Predicate<int> |
| Anonymous method | Any | Defined by delegate | delegate(…) {…} |
| Lambda expression | Any | Defined by delegate | () => {…} |
Best Practices for Delegate in C#
- Use built-in delegates whenever possible.
- Use lambdas to make delegate assignments more simple.
- Use events based on delegates for publish/subscribe patterns.
- Give your delegates clear and meaningful names.
Key Takeaways
- Delegates in C# let you pass methods as parameters.
- Delegates make your code more reusable and easier to test.
- Multicast delegates can call multiple methods at once.
- Action, Func, and Predicate make using delegates easier.
- Events in C# use delegates for event-driven programming.
Frequently Asked Questions
Q1: What is the main use of delegate in C#?
Delegates are mainly used for callbacks, events, and passing methods as parameters.
Q2: Is a delegate in C# the same as a function pointer?
They are similar, but delegates are type-safe, secure, and object-oriented.
Q3: Can a delegate in C# return a value?
Yes, using Func<> you can return values from a delegate call.
Q4: What is the difference between a delegate and an event in C#?
A delegate is a reference to a method, while an event is a wrapper that limits how delegates are used for publish/subscribe scenarios.
Q5: Should I use delegates or interfaces in C#?
Use delegates for callbacks and events. Use interfaces for defining design contracts.






