vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, October 15, 2012

Delegates and Multicast Delegates with example code in C# and VB


Delegate:
In C# delegate object used for mainly for two purpose
1. CallBack
2. Event Handling
Callback is nothing but mutual information exchange between two objects.
The common meaning of delegate is "one person act like another person".
Definition:
Delegate is a type which holds only the method reference, it’s doesn't need any information about the method definition.

Syntax for Delegate
Modifier delegate type_ofdelegate delegatename()
eg. public delegate string mydata(string var1,string var2)
Normally they are two types of delegate:
1. Delegates
2. Multicast Delegates
Rules:
When create delegate instance you must follow same return type and same parameter should follow.
Delegate Code in C#:
public delegate void Singledel(String str, String Str1);//Create instance
void addstring(string str,string str1)
    {   String str3;
        str3 = str + str1;
        Response.Write(str3);
    }
protected void Page_Load(object sender, EventArgs e)
    {
        Singledel firstdel = new Singledel(addstring);
        firstdel("DOTNET", "CODE");//Calling Delegate methods
}



Multicast Delegate:
Delegates hold multiple methods then it’s referred as multicast delegates. Multicast delegate have option to add multiple methods and also detached methods form the delegate.
Code in C#:
public delegate int multidel(int data1, int data2);
public  int add(int data1,int data2)
    {
        int data3 = data1 + data2;
        Response.Write(data3);
        return data3;
    }
   public int sub(int data1, int data2)
   {
       int data3 = data1 - data2;
       Response.Write( data3);
       return data3;
   }
protected void Page_Load(object sender, EventArgs e)
    {
multidel seconddel = new multidel(add);
        seconddel += sub;
      seconddel(5, 2);
}

When calling multicast delegate, encapsulated reference method has been executed.
In case multicast delegate have return type method, the value returned by the last method becomes the return value of entire invocation methods.

 Code In VB:
Private Delegate Sub Add(ByVal x As Integer)
Public Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer
        MsgBox(a + a)
        Return a + a
    End Function

    Public Function multiply(ByVal c As Integer, ByVal d As Integer) As Integer
        MsgBox(c * d)
        Return c * d
    End Function
Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Dim result As String
        Dim a As Deltest = New Deltest(AddressOf Sum)
        Dim b As Deltest = New Deltest(AddressOf multiply)

        Dim multicast As Deltest = DirectCast([Delegate].Combine(a, b), Delegvb.Deltest)

        result = multicast(3, 4)
        Response.Write(result)
    End Sub

Advantages
Callback mechanism
Asynchronous programming
Sequential programming etc.
Encapsulating the method's call from caller

I hope you like this article…..
Share this article with your friends by using share button…………

0 comments:

Post a Comment