Share Blog

Monday, April 21, 2014

Delegate in C#.net And Asp.net

Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. It is very similar to the function pointer but with a difference that delegates are a type-safe. We can say that it is the object-oriented implementation of function pointers.

There are three steps for defining and using delegates:

1.               Declaration

A delegate is declared by using the keyword delegate, otherwise it resembles a method declaration.

2.               Instantiation

To create a delegate instance, we need to assign a method (which has same signature as delegate) to delegate.

3.               Invocation

Invoking a delegate is like as invoking a regular method.

Types of delegates

1.               Single cast delegate

A single cast delegate holds the reference of only single method. In previous example, created delegate is a single cast delegate.

2.               Multi cast delegate

A delegate which holds the reference of more than one method is called multi-cast delegate. A multicast delegate only contains the reference of methods which return type is void. The + and += operators are used to combine delegate instances. Multicast delegates are considered equal if they reference the same methods in the same order.

1.               Single cast delegate Example:

Step- New Project ->New Console Application-> Write code


using System;
namespace Delegate_example
{
    //Declaration
    public delegate int mydelegate(int a, int b);//delegates having same signature as method 

    public class del
    {
        public int Sum(int a, int b)
        {
            return a + b;
        }
        public int Sub(int a, int b)
        {
            return a - b;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            del o = new del();
            // Instantiation : As a single cast delegate
            mydelegate sum = new mydelegate(o.Sum);
            mydelegate sub = new mydelegate(o.Sub);
            //Invocation
            Console.WriteLine("Sum Of The Two Number"+sum(20,10));
            Console.WriteLine("Sub of the two number"+sub(20,10));
           Console.ReadLine();
        }
    }
}

2.    Multi cast delegate Example


using System;

namespace multicast_delegate
{  
    public delegate void MyDelegate(int a,int b);

    class Example
    {
        public void Sum(int a,int b)
        {
            Console.WriteLine("Sum Of The Number   "+(a+b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Sub Of The Number    " +(a-b));
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Example ob = new Example();
            MyDelegate multicast = new MyDelegate(ob.Sum);
            multicast += new MyDelegate(ob.Sub);
            multicast(50,10);
        }
    }
}

2.    Multi cast delegate Another Example Using Asp.net

using System;

public partial class _Default : System.Web.UI.Page
{
    public delegate void mydele(int a,int b);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        mydele ob = Sum;
        ob += Sub;
        ob += Mul;
        ob += Div;
        ob.Invoke(int.Parse(TextBox1.Text),int.Parse(TextBox2.Text));
        

    }
    public void Sum(int a,int b)
    {
        Label1.Text = (a + b).ToString();
    }
    public void Sub(int a, int b)
    {
        Label2.Text = (a - b).ToString();
    }
    public void Mul(int a, int b)
    {
        Label3.Text = (a *b).ToString();
    }
    public void Div(int a, int b)
    {
        Label4.Text = (a /b).ToString();
    }
}


////////RESULT










No comments:

Post a Comment