Monday, 28 January 2013

How To Use Delegate and Its Type


For Asp.net Training with C# Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com

                                     How To Use Delegate and Its Type

Delegate is a reference type which refer to a method. Delegates object hold the reference of method of same signature. We invoke the method through delegate and this can be determined at the runtime instead of compile time.
Note:-delegates can invoke both static and non static method.

Syntax of delegate:-

Public delegate return_typ delegate_name(parameters);

For Example:-

For non static method
Suppose we are creating a class which contains some method for sum, div, sub and mul of two numbers. We create delegate to hold the reference of these methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


//delegate for holding method of same signature

public delegate int calculation(int x, int y);
public class delegatePract
{
    public int sum(int p, int q)
    {

        return p + q;
   
    }
    public int mul(int p, int q)
    {

        return p * q;

    }
    public int div(int p, int q)
    {

        return p / q;

    }
    public int sub(int p, int q)
    {

        return p - q;

    }
}
Now create web form and in page load create the object of class which holds the method and create the object of delegate and pass the ref to this delegate and call the method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        delegatePract dp=new delegatePract();
        calculation cal = new calculation(dp.sum);
        Response.Write(cal(5, 6));
    }
}

In this example first we create the object of class and after that we pass the reference of sum method to the delegate object and invoke the method which return the sum as we mentioned this definition in the method.
For static method
Now create the class which contains the static method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public delegate int calculation(int x, int y);
public class delegatePract
{
    public static int sum(int p, int q)
    {

        return p + q;
   
    }
    public static int mul(int p, int q)
    {

        return p * q;

    }
    public static int div(int p, int q)
    {

        return p / q;

    }
    public static int sub(int p, int q)
    {

        return p - q;

    }


}

Now create the object of delegate in page load and pass the reference of method with the class name as we create static method and we can call static method with class name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //creating the object of delegation
        calculation cal = new calculation(delegatePract.sum);
        Response.Write(cal(5, 6));
      
    }
}

Type of Delegates:-
1.     Single Cast Delegates:- single cast delegates are those delegates which holds the reference of single method as shown in the above example. It is derives from the System.Delegate name space.

2.     Multi cast Delegates:-multicast delegates contains the reference of multiple methods in series. We have to use the + - and = operator to add method and remove method from this series.

For example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        delegatePract dp=new delegatePract();
        calculation cal = new calculation(dp.sum);
        Response.Write(cal(5, 6));
        cal += dp.mul;
        cal += dp.sub;
        cal -= dp.mul;
        Response.Write(cal(6, 6));

    }
}


3 comments:

  1. Webtrackker Technology is a leading mobile application development company offering integrated, dynamic and streamlined e-commerce solutions to businesses worldwide. Webtrackker takes pride in developing outstanding mobile applications using Android and iOS based platforms. We have created variety of core Android, core iOS and hybrid applications.

    Mobile Apps Development Company in Delhi NCR

    ReplyDelete
  2. PHP Training in Noida - We provides better PHP course covering the entire course content from basics to the advanced level. It’s better to choose the classroom PHP training that provides the PHP course in noida to get the practical knowledge of working experience.

    ReplyDelete
  3. Java Training in Noida - Croma campus by excellent experienced IT professionals who has more then 10+ Years of real time experience Our trainers has good training actuality so that best quality output will be delivered.s

    ReplyDelete