In the C# programming language, a delegate is a reference type variable that holds a reference to a method. It functions similarly to a function pointer in C and C++. It offers a more secure, type-safe, and object-oriented approach. It allows methods to be passed as parameters, assigned to variables, and invoked dynamically at runtime. It is one of the most powerful and common features of the C# programming language that is used for implementing events.

In C#, a delegate can encapsulate both a static method and an instance method. Internally, a delegate declaration defines a class that is the derived class of System.Delegate.
Delegate type can be declared using the delegate keyword along with a method signature. If we declare a delegate, the delegate instance will refer to and invoke those methods which has the same return type and parameter list as the delegate declaration.
It has the following syntax:
In this syntax,
Once a delegate is declared in C#, we can instantiate it by assigning a method (using the new keyword) that matches its signature. It allows us to call that method indirectly through the delegate instance.
It has the following syntax.
Let us take an example to illustrate the delegate in C#.
Output:
Hello, Albert
Explanation:
In this example, we have taken a delegate named MyDel that contains the Point() method, which takes a string parameter. After that, the Point() method matches this signature and prints the message. Inside the main method, the delegate is instantiated with the Point method and calls the Point() method to print the output.
In the C# programming language, we can pass a delegate as a parameter to a method. It allows the method referenced by the delegate to be executed. After doing this, we can define a flexible and reusable method that can accept different functions as arguments.
Let us take an example to illustrate the delegate using a passing-through parameter in C#.
Output:
Welcome to TPointTech, David
Explanation:
In this example, we have taken a delegate named My_Del that contains the Say() method. The Say() method matches this delegate and prints a message. The GreetUser() method accepts a delegate and a string. Inside the main method, we create an instance of the delegate and assign it the Say() method, and then we pass this delegate along with the name "David" to GreetUser(). Finally, we call the Say() method and print the message.
In the C# programming language, a multicast delegate is a special type of delegate that can hold references to multiple methods at the same time. When the delegate is invoked, it executes all the methods one after another. It is useful for calling many functions using a single delegate. All the methods must have the same signature and return type.
Let us take an example to illustrate the multicast delegate in C#.
Output:
Welcome to TPointTech, David Welcome to TPointTech, David
Explanation:
In this example, we have taken a delegate named My_Del that contains the string parameter and two methods, SayHello() and Say(). Inside the main method, we create two instance d1 and d2 that are assigned to the Say method, and then we use the multicast delegate instances to print the output.
A generic delegate in C# is a type of delegate that is defined with a type parameter, which enables it to work with any type of data. We can also define a delegate without fixing the data type, which makes the code more flexible and reusable.
Let us take an example to illustrate the generic delegate in C#.
Output:
The square of a number is 25 Welcome, Alice
Explanation:
In this example, we have taken the generic delegate MyDel<T> that contains one parameter of type T and returns the same type. Inside the main method, we create two instances of delegate: one for int to calculate the square of a number using the Sq method, and another for string to greet a user using the Greet method, which returns a welcome message.
The following table displays the predefined generic delegates available in C#.
| Delegate | Description | Example |
|---|---|---|
| Action<T> | It defines a method that takes parameters but returns void. | Action<string> greet = name => Console.WriteLine("Hello, " +name); |
| Func<T, TResult> | It represents a method that takes a parameter with a return type. | Func<int, int, int> add = (m, n) => m + n; |
| Predicate<T> | It represents a method that takes a parameter and returns a Boolean type. | Predicate<int> isEven = x => x % 2 == 0; |
We request you to subscribe our newsletter for upcoming updates.