Thursday, September 2, 2010

Delegate

A delegate is a type that defines a method signature. When you create an instance of the delegate, you can associate a method to the instance of compatible signature, now you can call or invoke that method by the delegate instance.
Delegates are used to pass a method as argument to other method. A delegate instance can be assigned a method of any accessible class or struct that matches signature of delegate, method can be static or instance method.
Ex.
Public Delegate int myDelegate(int x, int y); //Delegate declaration
Public int add(int a, int b) //Method definition
{
Return a+b;
}
myDelegate del = add;  //Delegate instance del assigned method ‘add’
                                   //(Equivalent to  myDelegate del = new myDelegate(add); )

del(12, 30); //call or invoke

General terms
1. Delegates are like function pointers in C or C++.
2. Delegates can be used to pass a method as argument to other method.
3. Delegates can be chained together, multiple methods can be called on a single event. When you call a delegate that is chained(added multiple delegates) then all the methods called on this single call this is called Multicasting or Chaining of delegates. All methods called in the sequence in which they added. 
4. Covariance allows a more derived return type method to be assign than delegate’s return type.
5. Contravariance allows less derived parameters type method to be assign than delegate’s parameter’s type.
6. Framework 2.0 introduced anonymous methods, which allows code blocks to be passed to delegate as parameter in place of separately defined method.
Code block may contain anonymous method or lambda expression. 

//Instantition of myDelegate with instance mdel assigned anonymous method
myDelegate mdel = delegate(int m, int n) {
//Code block
}
//Instantition of myDelegate with lambda expression
(input parameters) => expression //lambda expression


myDelegate mdel =  (int x, int y) => { Code block }
OR
myDelegate mdel =  (x, y) => { Code block }  //Compiler infer input types by own

mdel(20,100); //Call or invoke
7. Delegates types ex. del (instance of delegate - myDelegate del; ) are drived from Delegate class, delegate types are sealed they can not be derived from.
8. When you invoke delegate, parameters passed to the delegate, pass to the method and returns value (if have return type) through delegate, its not possible to create custom class from Delegate class.
9. Instance of delegate is an object so it can be assign to a property or can be pass to a method as parameter and call the delegate at some later time this is called
Asynchronous callback.
10. Delegate can be assign static method.
11. A delegate can be chained (multicast) with static and instance both type methods together.


In C# 1.0 and later, delegates can be declared as shown in the following example.
// Declare a delegate.
delegate void Del(string str);

// Declare a method with the same signature as the delegate.
static void Notify(string name)
{
    Console.WriteLine("Notification received for: {0}", name);
}
// Create an instance of the delegate.
Del del1 = new Del(Notify);

C# 2.0 provides a simpler way to write the previous declaration, as shown in the following example.
// C# 2.0 provides a simpler way to declare an instance of Del.
Del del2 = Notify;

In C# 2.0 and later, it is also possible to use an anonymous method to declare and initialize a delegate, as shown in the following example.
// Instantiate Del by using an anonymous method.
Del del3 = delegate(string name)
    { Console.WriteLine("Notification received for: {0}", name); };

In C# 3.0 and later, delegates can also be declared and instantiated by using a lambda expression, as shown in the following example.
// Instantiate Del by using a lambda expression.
Del del4 = name =>  { Console.WriteLine("Notification received for: {0}", name); };

Combining or multicasting of delegates
Public Delegate void Del(String s);
Class Test
{
Static void hello(string s)
{
System.Console.WriteLine(“Hello {0}!”,s);
}

Static void Goodbye(string s)
{
System.Console.WriteLine(“Good Bye {0}!”,s);
}

Static void main()
{
Del a, b, c, d;
a = hello; //Assigning hello method to delegate a

b = Goodbye; //Assigning hello method to delegate a

c = a + b; // ( OR c = a; c += b;)

d = c – b; // ( OR d = c; d -= b;)
a(“Ram”); // Invoke delegate a
b(“Shyam”); // Invoke delegate b
c(“Ravi”); // Invoke delegates a then b
d(“Shubham”); // Invoke delegate a
}
}
Output
Hello Ram! //result of a
Good Bye Shyam! //result of b
Hello Ravi!     //result of c
Good Bye Ravi!
Hello Shubham! //result of d

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...