A delegate is a type that represents a reference to a function or method. Delegates allow methods to be passed as parameters, assigned to variables, and executed dynamically at runtime.
To do this, the delegate must match the signature (parameters received) of the function, and its specific return type.
In simple terms, a delegate can be thought of as a kind of function pointer, but with the safety and robustness of C#‘s type system.
If you want to learn more, check out the Introduction to Programming Course
Declaring a delegate
To declare a delegate in C# the delegate keyword is used, followed by the signature of the method the delegate will represent.
delegate ReturnType MyDelegate(ParameterType parameter1...);
- ReturnType, the return type of the function we want to delegate
- Type parameter…, the parameters of the function we want to delegate
For example, if we want to declare a delegate that represents a method that returns no value and takes two integer parameters, the declaration would look like this:
Basic example
For example, if we wanted to define a delegate that can point to any function that takes a string as a parameter and returns no value (void), we could do it like this:
public delegate void MyDelegate(string message);
And if we want to declare a delegate that references a function that takes two int parameters and returns a double, the declaration would look like this:
delegate void MyDelegate(int parameter1, int parameter2);
Using delegates
Once the delegate is declared, we can create an instance of the delegate and assign it a method that matches its signature.
To do this, we can create it using its constructor
MyDelegate delegate = new MyDelegate(ReferencedFunction);
Or simply assign it
MyDelegate delegate = ReferencedFunction;
Let’s see it with an example. Suppose we have the function ShowMessage(string ...).
// Method we want to reference
public static void ShowMessage(string message)
{
Console.WriteLine("Delegate: " + message);
}
// Delegate definition
public delegate void MyDelegate(string message);
public static void Main(string[] args)
{
// Now we create a MyDelegate that points to ShowMessage
MyDelegate delegate = ShowMessage;
}
Here we have,
- Defined a delegate
MyDelegate, which matches the form ofShowMessage. - Created a new instance of
MyDelegate, calleddelegate - Assigned the instance
delegatetoShowMessage.
Invoking a delegate
Once we have defined the delegate, created a delegate, and referenced a function with it, we can invoke it simply by doing () as we would with the original function.
In the previous example, we could invoke delegate simply like this.
// Invoking the method through the delegate
delegate("Hello, World!");
}
So it would be displayed on screen
Delegate: Hello, World!
Generic delegates: Func and Action
To simplify the use of delegates, C# provides predefined generic delegates.
The most common ones are Action and Func.
Action: Represents a method that does not return a valueFunc<TResult>: Represents a method that returns a value of typeTResult
Using Action
Action<string> show = message => Console.WriteLine(message);
show("Hello with Action!");
Using Func
Func<int, int, int> add = (a, b) => a + b;
int result = add(3, 4);
Console.WriteLine("Result of the addition: " + result);
In general, this form is what you will normally use, as opposed to defining the delegate explicitly. Especially if it’s a temporary use, not worth defining in the traditional way.
Multicast Delegates
A Multicast delegate is a delegate that can have more than one method in its invocation. In reality, all C# delegates are Multicast.
If you want to know more, check out this entry
Using delegates with events
Delegates are the foundation of events in C#. An event is a notification sent by an object to signal the occurrence of an action. Delegates allow subscribing methods that will be called when the event is fired.
If you want to know more, check out this entry
Practical examples
Delegate that returns nothing
Here is an example of a delegate that returns nothing (void).
// Declaration of the delegate that does not return anything
public delegate void ShowMessageDelegate(string message);
// Method that matches the signature of the delegate
public static void ShowMessage(string message)
{
Console.WriteLine(message); // Print the message
}
// Usage
ShowMessageDelegate showMessage = ShowMessage; // Assign the method to the delegate
showMessage("Hello, World!"); // Invoke the delegate
Delegate that returns a value
Here is an example of a delegate that returns a value. In the example, the delegate takes two integers as parameters and returns an integer.
// Declaration of the delegate that returns a value
public delegate int AddDelegate(int a, int b);
// Method that matches the signature of the delegate
public static int Add(int a, int b)
{
return a + b; // Return the sum of the two numbers
}
// Usage
AddDelegate add = Add; // Assign the method to the delegate
int result = add(3, 5); // Invoke the delegate and get the result
Console.WriteLine($"Result of the addition: {result}"); // Print the result
Generic Action delegate
Let’s see an example that uses a generic delegate Action<T> to represent a method that does not return a value and can take parameters.
// Method that matches the signature of Action
public static void PrintMessage(string message)
{
Console.WriteLine(message); // Print the message
}
// Usage
Action<string> print = PrintMessage; // Assign the method to the Action delegate
print("This is a message using Action."); // Invoke the delegate
Generic Func delegate
Now an example of a generic delegate Func<T> that can represent a method that returns a value and can take parameters.
// Method that matches the signature of Func
public static double CalculateCircleArea(double radius)
{
return Math.PI * radius * radius; // Calculate and return the area of the circle
}
// Usage
Func<double, double> calculateArea = CalculateCircleArea; // Assign the method to the Func delegate
double area = calculateArea(5.0); // Invoke the delegate and get the result
Console.WriteLine($"Area of the circle: {area}"); // Print the area of the circle
Delegate as a parameter in a Method
In this example, we will see how to pass a delegate as a parameter to another method.
// Declaration of the delegate that does not return anything
public delegate void OperationDelegate(int x, int y);
// Method that uses a delegate as a parameter
public static void PerformOperation(int a, int b, OperationDelegate operation)
{
operation(a, b); // Invoke the delegate
}
// Methods that match the signature of the delegate
public static void Add(int a, int b)
{
Console.WriteLine($"Sum: {a + b}");
}
public static void Multiply(int a, int b)
{
Console.WriteLine($"Product: {a * b}");
}
// Usage
OperationDelegate operationAdd = Add; // Assign the Add method to the delegate
OperationDelegate operationMultiply = Multiply; // Assign the Multiply method to the delegate
PerformOperation(3, 4, operationAdd); // Pass the delegate to the method
PerformOperation(3, 4, operationMultiply); // Pass the delegate to the method
