Delegates in C sharp
Write short notes on Delegates in C sharp .
Delegate is a method template which used to implement the concept of function pointer. The C and C++ languages are having the concept of function pointer. This was even more useful when programming for the Microsoft Windows operating systems because the Win32 library relies on the concept of callback functions. Callback functions are used in Microsoft Windows programming to process messages. For this reason and because of their functionality, callback functions were carried out in the .NET Framework but they were defined with the name of delegate.~ Concept
A delegate is a special type of user-defined variable that is declared globally, like a class.
In fact, a delegate is created like an interface but as a method. Based on this, a delegate interface, a delegate is not defined. Its role is to show what a useful method would look like.To support this concept, a delegate can provide all the necessary information that would be used on a method. This includes a return type, no argument or one or more Argument.
Delegate Declaration and Instantiation
Delegates can be specified in their own namespace, or else can 'be specified within other class. In each case, the declaration specifies a new class, which inherits from System.MulticastDelegate.
Each delegate is limited to referencing methods of a particular kind only. The type is indicated by the delegate declaration - the input parameters and return type given in the delegate declaration must be shared by the methods its delegate instances reference. To illustrate this: a delegate specified as below can be used to refer only to methods which have a single String input and no return value:
public delegate void print (String s);
Suppose, for instance, that a class contains the following method:
public void realMethod (String myString)
{
// method code
}
Another method in this class could then instantiate the 'Print' delegate in the following way, so that it holds a reference to 'realMethod':
print delegatevariable = new print(realMethod);
We can note two important points about this example. Firstly, the unqualified method passed to the delegate constructor is implicitly recognised as a method of the instance passing it. That is, the code is equivalent to:
Print delegatevariable = new print (this.realMethod);
We can, however, in the same way pass to the delegate constructor the methods of other class instances, or even static class methods. In the case of the former, the instance must exist at the time the method reference is passed. In the case of the latter (exemplified below), the class need never be instantiated.
Print delegatevariable = new
print(ExampleClass.exampleMethod);
Use of delegate
using System;
//delegate declaration
delegate int operation(int x, int y);
class MathOpr
{
public static int Add(int a,
{
return(a + b); }
public static int Sub(int a,
{
return( a - b); }
public static int Mul(int a,
{
return( a * b);
}
}
class Test
{
public static void Main () {
}
//delegate instances
operation opr1 = new operation (Mathopr.Add);
operation opr2 = new operation (Mathopr.sub);
operation opr3 = new operation (Mathopr.Mul); //invoking of delegates
int ans1 = opr1(200, 100);
int ans2 = opr2(200, 100);
int ans3 = opr3(20,10);
Console.writeLine(" Addition :"+ ans1);
console.writeLine(" Subtraction :"+ ans2);
Console.writeLine(" Multiplication :"+ ans3);
}
}
OUTPUT:
Adiition: 300
Subtraction: 100
Multiplication: 200
Multicast Delegates
The second thing to note about the example is that all delegates can be constructed in this fashion, to create a delegate instance which refers to. a single method. However, as we noted before, some delegates-termed 'multicast delegates' - can simultaneously reference multiple methods. These delegates must-like our Print delegate - specify a 'void' return type•
One manipulates the references of multicast delegates by using addition and subtraction.
The following code gives some examples:
print s = null;
s = s + new print (realMethod);
s += new print (otherRealMethod);
The - and - = operators are used in the same way to remove method references from a delegate.
The following code gives an example of the use of multicast delegates.
Use of Multicast delegates
using system;
delegate void Multiel();
class MD
{
static public void Hello()
{
console.writeLine("Hello");
}
static public void show()
{
console.writeLine(" Hi");
}
}
class Test
{
public static void Main()
{
//delegate instances
Multioel Ml = new Multioel(Mo.Hello);
Multioel M2 = new Multioel(Mo.hello);
}
/combine the delegates
Multioel M3 = Ml + M2;
Multioel M4 = M2 + Ml;
//extracting the delegates
Multioel M5 = M3 - M2;
Multioel M6 = M4 - Ml;
//invoking delegates
M3 () ;
M4();
M5();
M6();
}
}
OUTPUT:
Hello
Hi
Hi
Hello
hello
Hi
Delegates vs. Interface
Delegates and interfaces are similar in that they enable the aspiration of specification and implementation. Multiple independent authors can produce implementations that are compatible with an interface specification. Similarly, a delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification. When should you use interfaces, and when should you use delegates?
Delegates are useful when
• A single method is being called.
• A class may want to have multiple implementations of the method specification.
• It is desirable to allow using a static method to implement the specification.
• An event like design pattern is desired.
• The caller has no need to know or obtain the object that the method is defined on.
• The provider of the implementation wants to "hand out" the implementation of the specification to only a few selected components.
• Easy composition is desired
Interfaces are useful when:
• The specification defines a set of related methods that will be called.
, • A class typically implements the specification only one.
• The caller of the interface wants to cast to or from the interface to obtain interface or classes.