| by Arround The Web | No comments

Delegates in C# – How to Use It

Delegates are often used in C# to implement events and callbacks. By creating a delegate that can be assigned to a method, you can create a mechanism for other parts of your code to be notified when a particular event occurs, or to provide a method to be called when a certain action is complete. Delegates are a powerful tool in the C# language and are an essential part of many advanced programming techniques.

Delegates in C#

Delegate is similar to a function pointer in C or C++, but with additional capabilities such as combining multiple methods into a single delegate, invoking a list of methods in sequence, and passing parameters to the methods, here is the syntax for it:

public delegate <return-type> <delegate-name> (<parameters>)

 

In the above syntax, <return-type> is the return type of the method that the delegate points to. <delegate-name> is the name of the delegate, and <parameters> are the parameters that the method takes.

How to use Delegates in C#

To use a delegate, you must define a delegate type first, that specifies the signature of the method or methods it can reference. A delegate type can be defined using the delegate keyword, followed by the return type, the delegate name, and the parameter list for the method or methods it can reference. To illustrate the use of delegates in C sharp I have given an example code that perform simple addition and subtraction:

using System;

public delegate int PerformCalculation(int x, int y);

public class Calculator
{
    public int Add(int x, int y)
    {
        return x + y;
    }
    public int Subtract(int x, int y)
    {
        return x - y;
    }
}
class Program
{
    static void Main(string[] args)
    {
                Calculator calculator = new Calculator();
        PerformCalculation calculationDelegate = calculator.Add;
        int result = calculationDelegate(5, 3);
        Console.WriteLine($"Addition = {result}");
        calculationDelegate = calculator.Subtract;
        result = calculationDelegate(5, 3);
        Console.WriteLine($"subtraction = {result}");
        Console.ReadLine();
    }
}

 

First the delegate PerformCalculation is defined that takes two integer parameters and in returns gives an integer. It also defines a Calculator class with two methods: Add and Subtract that perform addition and subtraction operations respectively.

In the Main method, an instance of the Calculator class is created and then, the Add method is assigned to a delegate variable calculationDelegate. This means that calculationDelegate can now be used to call the Add method of the Calculator class. The Add method is called using calculationDelegate with arguments 5 and 3.

Next, the calculationDelegate is assigned to the Subtract method of the Calculator class. This means that calculationDelegate can now be used to call the Subtract method of the Calculator class. The Subtract method is called using calculationDelegate with arguments 5 and 3. The ReadLine() function is called to prevent the console window from closing immediately, here is the output of the code:

Conclusion

Delegates are commonly used in C# for event handling, callback functions, and as a way to pass functions as arguments to other functions. They provide a flexible way to handle functions in an object-oriented way, making it easier to write a code.

Share Button

Source: linuxhint.com

Leave a Reply