C# Delegates and their types
Delegate is a type in C#. Just as class and interface are types, in C# programming language, delegate is a type.
TYPE AND NAMESPACE
We know that any type is declared and defined within a namespace. Just as classes and interfaces are defined within the namespace, delegates also are declared and defined within the namespace. It's not necessary that we should define delegates within the namespace. If we want, we can also declare and define delegates within a class. As we know that in nested type, one type contains another type. For example, if there is another class inside a class, then it is called nested class, in the same way, delegate also can be declared inside a class.
If a type is contained within another type, it is called a nested type.
A delegate is a type by which a method is called. As we know that there are usually two ways to call a method of a class. If the method is non-static, then that method is called by an object by creating the object of the class. But if there is a static method, we call that static method by its class name instead of object of the class. Apart from this, there is a third way to call a method which is nothing but by using delegate. Delegate is pointer to method. The method to be called is pointed by delegate.
What kind of methods, a delegate can call is defined in the declaration of the delegate. The declaration of a delegate is similar to method. As the method has a method signature, the delegate is declared using the method signature. The signature of a method consists of the return type of the method, the name of the method, and the parameters associated with that method. In this method signature, instead of the name of the method, we write the name of a suitable delegate and use the delegate keyword just before the return type, in this way the delegate gets declared. For example,
public void Add(int n1, int n2);
public void Sub(int n1, int n2);
public void Multiply(int n1, int n2);
The delegate corresponding to these three methods will be as follows
public delegate void CalcDelegate(int num1, int num2);
This delegate can call the method whose method signature matches with the method signature of this delegate. A delegate can call a method only if the return type of that method is the same as the return type of the delegate and the parameter of the method exactly matches the parameter of the delegate, i.e. the datatype of the parameter is the same number and order.
CalcDelegate is a delegate type that can hold references to methods whose signature matches the signature of the CalcDelegate.
As delegate is a type, we use the new operator to create an instance of it, just as a class instance is created with the new operator, in the same way, an instance of a delegate is created with the new operator. It is to be remembered that delegate is always given the name of the function as parameter to call that function. For example
CalcDelegate delAdd = new CalcDelegate(Add);
CalcDelegate delSub = new CalcDelegate(Sub);
CalcDelegate delMultiply = new CalcDelegate(Multiply);
Note that when an object of a delegate is created, only the name of the function is given and not the value of the parameters associated with that function.
The delAdd is an object of type CalcDelegateDelegate that holds a reference to the Add method. Similarly delSub is an object of type CalcDelegateDelegate that holds a reference to the Sub method and so on.
When a method has to be called with the help of delegate, then the delegate's object is invoked at that time. The value of the parameter is passed when invoking the method.
For example
delAdd.Invoke(2, 4);
delSub.Invoke(2, 4);
delMultiply.Invoke(2, 4);
Here Invoke is the method of a delegate object which calls the method based on the method reference held by that delegate object.
It can also be written in short form as follows
delAdd(2, 4);
delSub(2, 4);
delMultiply(2, 4);
MULTICAST DELEGATE
As we have seen that a delegate object refers to only one method. If we want multiple methods to be called by a delegate object, then multicast delegate has to be used for this. A multicast delegate is a delegate object that refers to and calls multiple methods. We can understand this with the help of example.
CalcDelegate delObj = Add;
delObj += Sub;
delObj += Multiply;
delObj.Invoke(2, 5);
In the above example three methods are called by delObj. Note that the delegate object can refer to a method without the new operator.
ANONYMOUS METHOD
Anonymous method is a topic associated with delegate. Anonymous method is a method that has no name, only the body of the method within which the logic related to the method's activities is written.
Then the question arises, how is the anonymous method called when it has no name at all.
The delegate keyword is used to call the anonymous method. The delegate keyword immediately before the body of the anonymous method is used. The body of the method is delimited by {}
CLICK: RELATED POST
Updated on 6 July 2023
No comments:
Post a Comment