C# Delegates
C# Delegates
C# - Delegates
What if we want to pass a function as a parameter? How does C# handles the callback functions
or event handler? The answer is - delegate.
The delegate is a reference type data type that defines the method signature. You can define
variables of delegate, just like other data type, that can refer to any method with the same
signature as the delegate.
1. Declare a delegate
2. Create an instance and reference a method
3. Invoke a delegate
A delegate can be declared using the delegate keyword followed by a function signature, as shown
below.
Delegate Syntax
Above, we have declared a delegate MyDelegate with a void return type and a string parameter. A
delegate can be declared outside of the class or inside the class. Practically, it should be declared
out of the class.
After declaring a delegate, we need to set the target method or a lambda expression. We can do it
by creating an object of the delegate using the new keyword and passing a method whose
signature matches the delegate signature.
// target method
static void MethodA(string message)
{
Console.WriteLine(message);
}
You can set the target method by assigning a method directly without creating an object of
delegate e.g., MyDelegate del = MethodA .
After setting a target method, a delegate can be invoked using the Invoke() method or using the
() operator.
del.Invoke("Hello World!");
// or
del("Hello World!");
Example: Delegate
public delegate void MyDelegate(string msg); //declaring a delegate
class Program
{
static void Main(string[] args)
{
MyDelegate del = ClassA.MethodA;
del("Hello World");
del = ClassB.MethodB;
del("Hello World");
class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with parameter: " + message);
}
}
class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with parameter: " + message);
}
}
Try it
C# Delegate
Passing Delegate as a Parameter
Example: Delegate
class Program
{
static void Main(string[] args)
{
MyDelegate del = ClassA.MethodA;
InvokeDelegate(del);
del = ClassB.MethodB;
InvokeDelegate(del);
class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with parameter: " + message);
}
}
class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with parameter: " + message);
}
}
Try it
Note:
In .NET, Func and Action types are built-in generic delegates that should be used for most common delegates instead of creating new
custom delegates.
Multicast Delegate
The delegate can point to multiple methods. A delegate that points multiple methods is called a
multicast delegate. The "+" or "+=" operator adds a function to the invocation list, and the "-"
and "-=" operator removes it.
Example: Multicast Delegate
class Program
{
static void Main(string[] args)
{
MyDelegate del1 = ClassA.MethodA;
MyDelegate del2 = ClassB.MethodB;
MyDelegate del3 = (string msg) => Console.WriteLine("Called lambda expression: " + msg);
del += del3; // combines del1 + del2 + del3
del("Hello World");
class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with parameter: " + message);
}
}
class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with parameter: " + message);
}
}
Try it
The addition and subtraction operators always work as part of the assignment: del1 += del2; is
exactly equivalent to del1 = del1+del2; and likewise for subtraction.
If a delegate returns a value, then the last assigned target method's value will be return when a
multicast delegate called.
class Program
{
static void Main(string[] args)
{
MyDelegate del1 = ClassA.MethodA;
MyDelegate del2 = ClassB.MethodB;
class ClassA
{
static int MethodA()
{
return 100;
}
}
class ClassB
{
static int MethodB()
{
return 200;
}
}
Try it
Generic Delegate
A generic delegate can be defined the same way as a delegate but using generic type parameters
or return type. The generic type must be specified when you set a target method.
For example, consider the following generic delegate that is used for int and string parameters.
class Program
{
static void Main(string[] args)
{
add<int> sum = Sum;
Console.WriteLine(sum(10, 20));
Try it
Points to Remember :
1) Delegate is the reference type data type that defines the signature.
2) Delegate type variable can refer to any method with the same signature as the delegate.
6) Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-="
operator. It is called multicast delegate.
7) If a multicast delegate returns a value then it returns the value from the last assigned target method.
Related Articles