C# delegate
last modified October 15, 2026
In this article we work with delegates in C#.
Delegate definition
A delegate is a form of type-safe method pointer. It references methods with a specific parameter list and return type.
A delegate is a reference type. But instead of referring to an object, a delegate refers to a method. We can invoke the referenced method through the delegate instance.
Delegates are used in the following cases:
- Event handlers
- Callbacks
- Passing methods as method parameters
- LINQ
- Implementation of design patterns
There is nothing that is done with delegates that cannot be done with regular methods. Delegates are used because they bring several advantages. They foster flexibility of the application and code reuse. When we need to decide which method to call at runtime, we use a delegate.
C# using delegates
The following simple example shows how to use a delegate.
var md = new MyDelegate(MyCallback);
md();
void MyCallback()
{
Console.WriteLine("Calling callback");
}
delegate void MyDelegate();
We declare a delegate, create an instance of the delegate and invoke it.
var md = new MyDelegate(MyCallback);
We create an instance of the delegate. When called, the delegate will
invoke the MyCallback method.
md();
We call the delegate.
delegate void MyDelegate();
This is our delegate declaration. It returns no value and takes no parameters. Type declarations must follow the top-level statements.
$ dotnet run Calling callback
We can use a different syntax for creating and using a delegate.
MyDelegate del = MyCallback;
del();
void MyCallback()
{
Console.WriteLine("Calling callback");
}
delegate void MyDelegate();
We can save some typing when creating an instance of a delegate.
MyDelegate del = MyCallback;
This is another way of creating a delegate. We point directly to the method name; the compiler creates the delegate instance for us. This is called a method group conversion.
C# delegate pointing to different methods
A delegate can point to different methods over time.
var per = new Person("Fabius", "Maximus");
var nDelegate = new NameDelegate(per.ShowFirstName);
nDelegate("Call 1:");
nDelegate = new NameDelegate(per.ShowSecondName);
nDelegate("Call 2:");
public delegate void NameDelegate(string msg);
public class Person
{
public string firstName;
public string secondName;
public Person(string firstName, string secondName)
{
this.firstName = firstName;
this.secondName = secondName;
}
public void ShowFirstName(string msg)
{
Console.WriteLine($"{msg} {this.firstName}");
}
public void ShowSecondName(string msg)
{
Console.WriteLine($"{msg} {this.secondName}");
}
}
In the example we have one delegate. This delegate is used to point to two
methods of the Person class. The methods are called with the
delegate.
var nDelegate = new NameDelegate(per.ShowFirstName);
nDelegate("Call 1:");
We create an instance of a new delegate that points to the
ShowFirstName method. Later we call the method via the delegate.
public delegate void NameDelegate(string msg);
The delegate is declared with the delegate keyword. The delegate
signature must match the signature of the method being called with the delegate.
$ dotnet run Call 1: Fabius Call 2: Maximus
Both names are printed via the delegate.
C# multicast delegate
A multicast delegate is a delegate which holds references to more than one
method. The methods are invoked in the order in which they were added. Methods
are added with the += operator and removed with the
-= operator.
var del = new MyDelegate(Oper.Add);
del += new MyDelegate(Oper.Sub);
del(6, 4);
del -= Oper.Sub;
del?.Invoke(2, 8);
delegate void MyDelegate(int x, int y);
public class Oper
{
public static void Add(int x, int y)
{
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
}
public static void Sub(int x, int y)
{
Console.WriteLine("{0} - {1} = {2}", x, y, x - y);
}
}
This is an example of a multicast delegate.
var del = new MyDelegate(Oper.Add);
We create an instance of our delegate. The delegate points to the static
Add method of the Oper class.
del += new MyDelegate(Oper.Sub); del(6, 4);
We add another method to the existing delegate instance. The first call of the delegate invokes two methods.
del -= Oper.Sub; del?.Invoke(2, 8);
We remove one method from the delegate. The second call of the delegate invokes
only one method. If we removed all methods, the delegate would become
null; therefore, the compiler warns us and we invoke the delegate
with the null-conditional operator.
delegate void MyDelegate(int x, int y);
Our delegate takes two parameters. We have an Oper class which has
two static methods. One adds two values, the other one subtracts two values.
$ dotnet run 6 + 4 = 10 6 - 4 = 2 2 + 8 = 10
Multicast delegates are typically used with methods that return
void. If the methods return a value, invoking the delegate returns
only the value of the last method in the invocation list.
var del = new MyDelegate(Oper.Add);
del += Oper.Mul;
int res = del(6, 4);
Console.WriteLine(res);
delegate int MyDelegate(int x, int y);
public class Oper
{
public static int Add(int x, int y) => x + y;
public static int Mul(int x, int y) => x * y;
}
Both methods are called, but the result of the Add method is
discarded.
$ dotnet run 24
C# anonymous methods
It is possible to use anonymous methods with delegates.
MyDelegate del = delegate
{
Console.WriteLine("Anonymous method");
};
del();
delegate void MyDelegate();
We can omit a method declaration when using an anonymous method with a delegate. The method has no name and can be invoked only via the delegate.
MyDelegate del = delegate
{
Console.WriteLine("Anonymous method");
};
Here we create a delegate that points to an anonymous method. The anonymous
method has a body enclosed by {} characters, but it has no name.
$ dotnet run Anonymous method
C# lambda expressions
In modern C#, anonymous methods are mostly replaced with lambda expressions, which offer a more concise syntax.
MyDelegate del = () => Console.WriteLine("Lambda expression");
del();
Arithm add = (x, y) => x + y;
Console.WriteLine(add(3, 5));
delegate void MyDelegate();
delegate int Arithm(int x, int y);
We assign two lambda expressions to delegates. The types of the
x and y parameters are inferred from the
Arithm delegate.
$ dotnet run Lambda expression 8
C# delegates as method parameters
Delegates can be used as method parameters.
DoOperation(10, 2, Multiply);
DoOperation(10, 2, Divide);
void DoOperation(int x, int y, Arithm del)
{
int z = del(x, y);
Console.WriteLine(z);
}
int Multiply(int x, int y)
{
return x * y;
}
int Divide(int x, int y)
{
return x / y;
}
delegate int Arithm(int x, int y);
We have a DoOperation method which takes a delegate as a parameter.
DoOperation(10, 2, Multiply); DoOperation(10, 2, Divide);
We call the DoOperation method. We pass two values and a method
to it. What we do with the two values depends on the method that we pass. This
is the flexibility that comes with using delegates.
void DoOperation(int x, int y, Arithm del)
{
int z = del(x, y);
Console.WriteLine(z);
}
This is DoOperation method implementation. The third parameter is
a delegate. The DoOperation method calls a method which is passed
to it as a third parameter.
delegate int Arithm(int x, int y);
This is a delegate declaration.
$ dotnet run 20 5
C# events
Events are messages triggered by some action. A click on a button or a tick of a clock are such actions. The object that triggers an event is called a sender and the object that receives the event is called a receiver.
By convention, event delegates in .NET have two parameters: the source that raised the event and the data for the event. An event can be invoked only from within the class that declares it.
var fe = new FEvent();
fe.FiveEvent += Callback;
var random = new Random();
for (int i = 0; i < 10; i++)
{
int rn = random.Next(6);
Console.WriteLine(rn);
if (rn == 5)
{
fe.OnFiveEvent();
}
}
void Callback(object? sender, EventArgs e)
{
Console.WriteLine("Five Event occurred");
}
class FEvent
{
public event OnFiveHandler? FiveEvent;
public void OnFiveEvent()
{
FiveEvent?.Invoke(this, EventArgs.Empty);
}
}
public delegate void OnFiveHandler(object? sender, EventArgs e);
We have a simple example in which we create and launch an event. A random
number is generated. If the number equals to 5, a FiveEvent event
is generated.
fe.FiveEvent += Callback;
Here we subscribe the Callback method to the event called
FiveEvent. In other words, if the FiveEvent event is
triggered, the Callback method is executed.
public event OnFiveHandler? FiveEvent;
An event is declared with the event keyword. The event is
null if there are no subscribers; therefore, it is declared as
nullable.
public void OnFiveEvent()
{
FiveEvent?.Invoke(this, EventArgs.Empty);
}
When the random number equals to 5, we invoke the OnFiveEvent
method. In this method, we raise the FiveEvent event. The
null-conditional ?. operator ensures that the event is raised only
if it has some subscribers. This event carries no arguments.
$ dotnet run 1 2 0 3 5 Five Event occurred 5 Five Event occurred 5 Five Event occurred 0 2 5 Five Event occurred
C# complex event example
Next we have a more complex example. This time we send some data with the generated event.
namespace ComplexEvent;
public delegate void OnFiveHandler(object? sender, FiveEventArgs e);
public class FiveEventArgs : EventArgs
{
public int Count { get; }
public DateTime Time { get; }
public FiveEventArgs(int count, DateTime time)
{
Count = count;
Time = time;
}
}
public class FEvent
{
public event OnFiveHandler? FiveEvent;
public void OnFiveEvent(FiveEventArgs e)
{
FiveEvent?.Invoke(this, e);
}
}
public class RandomEventGenerator
{
public void Generate()
{
int count = 0;
var fe = new FEvent();
fe.FiveEvent += Callback;
var random = new Random();
for (int i = 0; i < 10; i++)
{
int rn = random.Next(6);
Console.WriteLine(rn);
if (rn == 5)
{
count++;
var args = new FiveEventArgs(count, DateTime.Now);
fe.OnFiveEvent(args);
}
}
}
public void Callback(object? sender, FiveEventArgs e)
{
Console.WriteLine($"Five event {e.Count} occurred at {e.Time}");
}
}
class Program
{
static void Main()
{
var reg = new RandomEventGenerator();
reg.Generate();
}
}
We have four classes. The FiveEventArgs carries some data with the
event object. The FEvent class encapsulates the event object. The
RandomEventGenerator class is responsible for random number generation.
It is the event sender. Finally, the Program class contains the
Main method, which is the entry point of the application. In this
example we do not use top-level statements.
public class FiveEventArgs : EventArgs
{
public int Count { get; }
public DateTime Time { get; }
...
The FiveEventArgs carries data inside the event object. It inherits
from the EventArgs base class. The Count and
Time properties are data that will be initialized and carried
with the event.
if (rn == 5)
{
count++;
var args = new FiveEventArgs(count, DateTime.Now);
fe.OnFiveEvent(args);
}
If the generated random number equals to 5, we instantiate the
FiveEventArgs class with the current count and
DateTime values. The count variable counts the number
of times this event was generated. The DateTime value holds the
time when the event was generated.
$ dotnet run 1 0 3 5 Five event 1 occurred at 10/15/2026 11:01:49 PM 2 3 5 Five event 2 occurred at 10/15/2026 11:01:49 PM 2 0 0
In practice, we often use the built-in generic EventHandler<TEventArgs>
delegate instead of declaring our own event delegate type.
C# predefined delegates
.NET has several built-in delegates that reduce the typing needed and make the programming easier for developers.
C# Func delegate
Func is a built-in generic delegate type. Func can be
used with a method, an anonymous method or a lambda expression.
Func can contain 0 to 16 input parameters and must have one return
type. (There are 17 variants of the Func delegate.)
public delegate TResult Func<in T, out TResult>(T arg);
For instance, this delegate encapsulates a method that has one parameter and
returns a value of the type specified by the TResult parameter.
string GetMessage()
{
return "Hello there!";
}
Func<string> sayHello = GetMessage;
Console.WriteLine(sayHello());
In the example, we use the Func delegate which has no parameters
and returns a single value.
$ dotnet run Hello there!
C# Action delegate
An action delegate encapsulates a method that has no parameters and does not return a value.
Action act = ShowMessage;
act();
void ShowMessage()
{
Console.WriteLine("C# language");
}
Using predefined delegates further simplifies programming. We do not need to declare a delegate type.
Action act = ShowMessage; act();
We instantiate an action delegate. The delegate points to
the ShowMessage method. When the delegate is invoked, the
ShowMessage method is executed.
$ dotnet run C# language
There are multiple types of action delegates. For example, the
Action<T> delegate encapsulates a method that takes a single
parameter and does not return a value.
Action<string> act = ShowMessage;
act("C# language");
void ShowMessage(string message)
{
Console.WriteLine(message);
}
We modify the previous example to use the action delegate that takes one parameter.
Action<string> act = ShowMessage;
act("C# language");
We create an instance of the Action<T> delegate and
call it with one parameter.
$ dotnet run C# language
C# Predicate delegate
A predicate is a method that returns true or false. A predicate delegate is a reference to a predicate. Predicates are very useful for filtering lists of values.
List<int> vals = [4, 2, 3, 0, 6, 7, 1, 9];
Predicate<int> myPred = GreaterThanThree;
List<int> vals2 = vals.FindAll(myPred);
foreach (int i in vals2)
{
Console.WriteLine(i);
}
bool GreaterThanThree(int x)
{
return x > 3;
}
We have a list of integer values. We want to filter all numbers that are bigger than three. For this, we use the predicate delegate.
List<int> vals = [4, 2, 3, 0, 6, 7, 1, 9];
This is a generic list of integer values created with a collection expression.
Predicate<int> myPred = GreaterThanThree;
We create an instance of a predicate delegate. The delegate points to a predicate, a special method that returns true or false.
List<int> vals2 = vals.FindAll(myPred);
The FindAll method retrieves all the elements that match the
conditions defined by the specified predicate.
bool GreaterThanThree(int x)
{
return x > 3;
}
The predicate returns true for all values that are greater than three.
$ dotnet run 4 6 7 9
Source
In this article we worked with delegates in C#.
Author
List all C# tutorials.