Wednesday, March 16, 2016

What is delegate in c#?



Delegate is a user defined data type, it is used to store the reference on method. C# delegate work same as c language pointer.

Use of the delegate: Use of the delegate is that when you have two or more methods with same name and same signature and call all the method with single object.

The example of the delegate is given below: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public delegate int mydel(int x, int y);
   public class Class1
    {
        public int add(int a, int b)
        {
            return a + b;
        }

        public int sub(int a, int b)
        {
            return a - b;
        }
    }
}

.cs code 

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 WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            mydel del = obj.add;
            int i = del(10, 20);
            MessageBox.Show(i.ToString());
            mydel del1 = obj.sub;
            int j = del1(20, 10);
            MessageBox.Show(j.ToString());
        }
    }
}
Output :
30
10




Types of delegate:
 
There are two types of delegate –
-Single cast delegates
-Multi cast delegates
Single cast Delegate – single cast delegate hold the address of single method like as explained in the above example.

Multicast Delegate - multicast delegate is used to hold the address of multiple methods, for this work we will use overloaded += operator and remove the address from delegate we use overloaded operator -=
Multicast Delegates will work for those methods which have return type only void.
The example of the Multicast delegates is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public delegate void mydel(int a, int b);
    public class Class1
    {
        public void add(int x, int y)
        {
            MessageBox.Show("addition value"+(x+y).ToString());
        }
        public void sub(int x, int y)
        {
            MessageBox.Show("subtraction value" + (x - y).ToString());
           
        }
        public void mul(int x, int y)
        {
            MessageBox.Show("multly value" + (x * y).ToString());
        }
    }
}


.cs code :
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 WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            mydel del = Class1.add;
            del += Class1.sub;
            del += Class1.mul;
            del(10, 5);



        }
    }
}

Output:
addition value : 15
subtraction value : 5
multiply value : 50



No comments:

Post a Comment