DEV Community

Hootan Hemmati
Hootan Hemmati

Posted on

Delegates in C#

Delegates are an important concept in C# programming language. They allow you to define a type-safe function pointer that can reference one or more methods with the same signature. This is useful when you want to pass a method as a parameter to another method, or when you want to store a reference to a method for later use.

In this article, we will cover the basics of delegates in C#, including how to declare, instantiate, and use them in your code.

What is a delegate?

In C#, a delegate is a reference type that represents a method with a specific signature. A delegate can be thought of as a type-safe function pointer, which means it points to a method with a specific signature and return type. You can pass a delegate to a method as a parameter or store a reference to it for later use.

Here's an example of a simple delegate declaration:

delegate void MyDelegate(string message);
Enter fullscreen mode Exit fullscreen mode

This declaration creates a delegate type called MyDelegate that takes a string parameter and returns void. You can use this delegate to reference any method that matches this signature.

Creating an instance of a delegate

To create an instance of a delegate, you need to provide it with a reference to a method that matches its signature. You can do this by using the new keyword and passing the name of the method you want to reference:

MyDelegate del = new MyDelegate(MyMethod);
Enter fullscreen mode Exit fullscreen mode

In this example, we create an instance of the MyDelegate delegate and pass it a reference to the MyMethod method. The MyMethod method should have the same signature as the MyDelegate delegate.

Invoking a delegate

Once you have created an instance of a delegate, you can invoke it just like you would invoke any other method. You can do this by using the name of the delegate and passing any required parameters:

del("Hello, world!");
Enter fullscreen mode Exit fullscreen mode

In this example, we invoke the del delegate and pass it a string parameter. The delegate will then invoke the method that it references, passing the same string parameter.

Combining delegates

You can also combine multiple delegates into a single delegate using the + operator. This is useful when you want to execute multiple methods at once.

MyDelegate del1 = new MyDelegate(Method1);
MyDelegate del2 = new MyDelegate(Method2);
MyDelegate del3 = del1 + del2;
Enter fullscreen mode Exit fullscreen mode

In this example, we create two instances of the MyDelegate delegate (del1 and del2) and then combine them into a single delegate (del3) using the + operator. When we invoke del3, both Method1 and Method2 will be executed in order.

Removing delegates

You can also remove a delegate from a multi-cast delegate by using the - operator. This is useful when you want to remove a specific method from a multi-cast delegate.

del3 = del3 - del1;
Enter fullscreen mode Exit fullscreen mode

In this example, we remove the del1 delegate from the del3 multi-cast delegate using the - operator. When we invoke del3 now, only Method2 will be executed.

Top comments (0)