In c#, the delegate is a type that defines a method signature, and it is useful to hold the reference of one or more methods which are having the same signatures.
By using delegates, you can invoke the methods and send methods as an argument to other methods.
In c#, the delegate is a reference type, and it’s type-safe and secure. The delegates are similar to function pointers in C++.
In c#, the declaration of delegate will be same as the method signature, but the only difference is we will use a delegate
keyword to define delegates.
Following is the syntax of defining a delegate using delegate
keyword in c# programming language.
Following is the example of declaring a delegate using delegate
keyword in c#.
If you observe the above example, a delegate's declaration is same as a method declaration with required parameter types and return value.
The above method “UserDetails” can be pointed to any other method with the same parameters and return type.
In c#, the delegates must be instantiated with a method or expression with the same return type and parameters. We can invoke a method through the delegate instance.
Following is the example of creating delegates in the c# programming language.
If you observe the above example, we created a delegate called “SampleDelegate” and the delegate has been instantiated by using defined methods.
When you execute the above c# program, you will get the result below.
This is how we can use delegates in our applications to call all the methods which are having the same signatures with a single object.
In c#, you can invoke the delegates same as the method or by using the Invoke method as shown below.
In c#, we have two different types of delegates available. Those are
In c#, a delegate that points to a single method is called a single cast delegate, and it is used to hold the reference of a single method as explained in the above example.
In c#, a delegate that points to multiple methods is called a multicast delegate, and it is used to hold the reference of multiple methods with a single delegate.
By using "+"
operator, we can add multiple method references to the delegate object. Same way, by using "-"
operator we can remove the method references from the delegate object.
In c#, Multicast delegates will work with only the methods that are having void
as return type. If we want to create a multicast delegate with the return type, then we will get a return type of the last method in the invoking list.
Following is the example of implementing a multicast delegate to hold the reference of multiple methods with "+"
operator in c# programming language.
If you observe the above example, we created a delegate called “SampleDelegate” and held the reference of multiple methods using "+"
operator. Our delegate becomes a multicast delegate, and invoking a dlgt instance will invoke all the methods sequentially.
When you execute the above c# program, you will get the result below.
This is how we can use multicast delegates in c# to hold the reference of multiple methods based on our requirements.
By using delegates in c#, we can pass methods as the parameter. Following is the example of sending methods as a parameter using a delegate.
If you observe the above example, we created a “SampleMethod” with a delegate as a parameter type. By declaring like this, you can pass the method as a parameter to the newly created method (SampleMethod).
When you execute the above c# program, you will get the result below.
The following are the important properties of delegate in c# programming language.
delegate
keyword to define delegates."+"
operator, we can add multiple methods to delegates.