C# Keywords Tutorial Part 21: delegate

C# Keywords Tutorial Part 21: delegate

Developers have a lot of freedom when it comes to defining functions and passing them around as variables thanks to C#’s powerful syntax. The “delegate” keyword, which enables programmers to define a type that represents a function with a particular signature, is one of the most helpful features in this respect. We’ll examine the C# “delegate” keyword in more detail and examine some usage examples in this blog article.

What is a delegate in C#?

Delegates are types in C# that indicate references to methods with particular signatures. To put it another way, it’s a method of treating functions like variables, enabling them to be passed around, given to variables, and even used as arguments to other functions. The “delegate” keyword is used to define a delegate, which is then followed by the method’s name, return type, and list of parameters.

Code Example:

delegate int Calculate(int x, int y);

In the above example, we have defined a delegate named “Calculate” that represents a method that takes two integer arguments and returns an integer value.

Creating and using delegates Once we have defined a delegate, we can create an instance of it and assign it a reference to a method that matches its signature. This can be done using the following syntax:

Calculate calculate = new Calculate(Add);

In this example, we have created an instance of the “Calculate” delegate and assigned it a reference to a method named “Add” that takes two integer arguments and returns an integer value.

Code Example:

public static int Add(int x, int y)
{
    return x + y;
}

Calculate calculate = new Calculate(Add);
int result = calculate(5, 10);
Console.WriteLine(result); // Output: 15

In the above example, we have defined a method named “Add” that takes two integer arguments and returns their sum. We then created an instance of the “Calculate” delegate and assigned it a reference to the “Add” method. Finally, we called the delegate with the arguments (5, 10), which caused the “Add” method to be invoked and return the sum, which was stored in the “result” variable.

Multicast delegates A delegate in C# can also be multicast, which means that it can have multiple methods assigned to it. When a multicast delegate is called, all the methods that have been assigned to it are called in the order in which they were added.

Code Example:

Calculate calculate = new Calculate(Add);
calculate += Subtract;
int result = calculate(10, 5);
Console.WriteLine(result); // Output: 5

In the above example, we have created an instance of the “Calculate” delegate and assigned it a reference to the “Add” method. We then added a reference to another method named “Subtract” using the += operator. When the delegate is called with the arguments (10, 5), both the “Add” and “Subtract” methods are invoked in the order in which they were added, resulting in a final value of 5.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *