Delegates in C# Language
Delegates are a fundamental concept in C# programming that allow you to work with methods as first-class obj
ects. They play a crucial role in event handling, callback mechanisms, and functional programming in C#. In this article, we’ll explore what delegates are, how they work, and provide examples to help you understand their practical applications.What is a Delegate?
A delegate in C# is a type that represents references to methods with a specific signature. Delegates enable you to treat methods as first-class objects, allowing you to pass them as parameters, store them in variables, and invoke them dynamically at runtime. Delegates serve as the foundation for event handling in C#.
Declaring a Delegate
To declare a delegate, you define its signature, which specifies the return type and parameters. Here’s a simple delegate declaration:
delegate int MyDelegate(int a, int b);
In this example, we’ve declared a delegate named MyDelegate
, which represents methods that take two integer parameters and return an integer.
Instantiating a Delegate
Once you’ve declared a delegate, you can create an instance of it and associate it with a method. Here’s an example:
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
// Instantiate the delegate and associate it with the Add method
MyDelegate addDelegate = new MyDelegate(new Calculator().Add);
In this code, we’ve created an instance of the MyDelegate
delegate and associated it with the Add
method of the Calculator
class.
Invoking a Delegate
You can use the delegate to invoke the associated method, just like calling the method directly:
int result = addDelegate(5, 3); // Calls the Add method and stores the result
Console.WriteLine("Result: " + result);
The addDelegate
instance is invoked with two integers, and it returns the sum, which is then printed to the console.
Multicast Delegates
C# supports multicast delegates, which can reference multiple methods. When a multicast delegate is invoked, all associated methods are called sequentially. Here’s an example:
delegate void MultiDelegate(int x);
void PrintNumber(int x)
{
Console.WriteLine("Number: " + x);
}
void SquareNumber(int x)
{
Console.WriteLine("Square: " + (x * x));
}
MultiDelegate multi = PrintNumber;
multi += SquareNumber; // Combines two methods
multi(5); // Calls both PrintNumber and SquareNumber
In this example, the multi
delegate references both the PrintNumber
and SquareNumber
methods. When you invoke it, both methods are called.
Built-in Delegates
C# provides several built-in delegates like Action
and Func
, which have predefined signatures. These are commonly used for various purposes. For instance, Action
represents a delegate that takes no parameters and returns no value, while Func
represents a delegate with up to 16 parameters and a return value.
Action<string> printMessage = message => Console.WriteLine(message);
Func<int, int, int> add = (a, b) => a + b;
printMessage("Hello, Delegates!");
int sum = add(3, 7);
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.