Sunday, 29 July 2012

Delegates in C# (Singlecast and Multicast delegates in C#)





Delegates  in C# .NET………………..

What is Delegates  in C #
Delegate is  the special type use to hold the reference of  function ,
Delegate  concept  match  the function pointer concept of  c language

Types Delegates  in C# : There  are two type delegate is  C#

       1.Single cast  Delegate
       2.Multicast Delegate

Single cast Delegates  in C #It is  those delegate which hold the  address of  single method,
               Setp 1: General Syntax to define the delegates

                 Access Specifier delegate Name of delegate in form of  function

 Note: In the   single cast delegate signature of delegate should be same as method for which we are creating for this delegate.

Like:  we create   functions………………………………………………………..

    public int sum(int x, int y)
    {
      return (x + y);
        
   }
    public double div(double x, double y)
    {
          return (x / y);
       
    }

Setp 2: After than  we  create delegate  for  above  function

          public delegate int sumdel(int x, int y);

          public delegate double devdel(double x, double y);


Setp 3: After Procedure to assign reference of function to a delegate

                a. Create object of delegate
                b. Set the object with name of function

           sumdel ob = new sumdel(sum);
           devdel ob1 = new devdel(div);

Note: ob hold the  address of sum method

Setp 4: Procedure to call  a function with  help  of  delegates…………….

                  ob(12,4)                     = 16;
                  ob1(12,4)                     = 3;



Example for Singlecast Delegates.........................



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyDelegateExp
{
    public partial class Form1 : Form
    {
       

        public Form1()
        {
            InitializeComponent();
        }
        public int sum(int x, int y)
        {
            return (x + y);
        }
        public double div(double x, double y)
        {
            return (x / y);
        }
        public int mul(int x, int y)
        {
            return (x * y);
        }
        public int sub(int x, int y)
        {
            return (x - y);
        }

        public delegate int sumdel(int x, int y);
        public delegate double devdel(double x, double y);
        public delegate int multdel(int x, int y);
        public delegate int subdel(int x, int y);


        private void ButtonSingleCast_Click(object sender, EventArgs e)
        {
            double i = Convert.ToDouble(txtfstno.Text);
            double j = Convert.ToDouble(txtsecondno.Text);
            int k = Convert.ToInt32(txtfstno.Text);
            int h = Convert.ToInt32(txtsecondno.Text);

            sumdel ob = new sumdel(sum);
            devdel ob1 = new devdel(div);
            multdel ob2 = new multdel(mul);
            subdel ob3 = new subdel(sub);

            textBox1.Text = ob(k, h).ToString();
            textBox2.Text = ob1(i, j).ToString();
            textBox3.Text = ob2(k, h).ToString();
            textBox4.Text = ob3(k, h).ToString();
        }

     
    }
}

Multicast  Delegates  in C #If you  want  to hold the address of  multiple method in a single delegates  then we  have  to declare  multicast  delegate,
                                          Multicast  delegate  will work only those  method  which have  return type  only  void………………

Like:  we  create   functions………………………………………………………..

      public void sum(int x, int y)
      {
           MessageBox.Show( (x + y).ToString());
       }
        public void div(int x, int y)
        {
        MessageBox.Show((x / y).ToString());
        }
        public void mul(int x, int y)
        {
         MessageBox.Show((x * y).ToString());
        }

       Setp 2: After than  we  create  multicast delegate  for  above  function

                   public delegate void Muldel(int x, int y);
        

      Setp 3: After Procedure to assign reference of  function to a delegate
a.  Create  object  of  delegate
               b. Set  the  object  with name  of function

                 Multicastdel ob = new Multicastdel(sum);
           
                      ob += div;
                      ob += mul;
                      ob += sub;
                      ob(k, h);

        
          Note: ob hold the  address of all methods

        Setp 4: Procedure to call  a function with  help  of  delegates…………….
         
                     ob(12,4)                   Result  = 16,3,48,8;







Example for Multicast Delegates.........................
                   



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyDelegateExp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void sum(int x, int y)
        {
            MessageBox.Show("Addition" + "  " + (x + y).ToString());
        }
        public void div(int x, int y)
        {
            MessageBox.Show("Division" + "  " + (x / y).ToString());
        }
        public void mul(int x, int y)
        {
            MessageBox.Show("Multiplication""  "+(x * y).ToString());
        }
        public void sub(int x, int y)
        {
            MessageBox.Show("Subtraction"+"  "+(x - y).ToString());
        }

        public delegate void Multicastdel(int x, int y);

        private void bMulCastDel_Click(object sender, EventArgs e)
        {
            int k = Convert.ToInt32(txtfstno.Text);
            int h = Convert.ToInt32(txtsecondno.Text);
            Multicastdel ob = new Multicastdel(sum);
          
            ob += div;
            ob += mul;
            ob += sub;
            ob(k, h);
           
        }

    
    }
}









9 comments:

  1. Hi,

    When we make an object of a class then we can access all function of a class and Base class functions too. Here we can see that a user can simple make an object of a

    class and access all information of class. But in real word this is drawback of an object.

    This is a nice article so thanks for sharing your knowledge. There are few other links that also described "Delegate in C#" in a good way.

    http://mindstick.com/Articles/2ace6310-943e-4f1b-a193-c1b5f61acd54/default.aspx?Delegate+in+C+Net

    http://www.dotnetperls.com/delegate

    http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C

    ReplyDelete
  2. nice concept sir it is very helpfull for me thanks

    ReplyDelete
  3. This is a nice article so thanks for sharing your knowledge. There are few other links that also described "Delegate in C#" in a good way.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. how can use the Anonymous method delegate...???????

    ReplyDelete