Delegate In C#

By
Delegate is an object type which can be used to Encapsulate the method call and handle the event.Through Delegate only that function can be called whose signature and  Return Type is same as DelegateDelegate basically helps for calling the other class method. Delegate can be declared within the class or outside the class.Delegate is like a method but it is use like a class to create the object of del class.
There are some feature of delegate in C#.

1. Declaration of delegate.
  Syntax:
    <Access specifier --> delegate keyword--> Return type --> delegate Name --> (Parameter list)>
Ex: Public delegate void del();
2. Declaration of  the delegate Method.
A method which will be called by the delegate is known as delegate method.
Ex:
public class student
{
public void show()
{
Console.WriteLine("Hello Friends");
Console.ReadLine();
}
}
3. Declaration of the delegate object.
Ex;
student obj = new student();
del delobj = new del(obj.show);
4. Call the delegate object.
delobj();

There can be two Types of delegate .
     1. Single cast delegate:- In this type of delegate one delegate object can call only method
Ex:-

using System;
using System.Text;
namespace @delegate
{
    class Program
    {
        public delegate void del(int p,int q);
        public class student
        {
            public void add(int a,int b)
            {
                int res = a + b;
                Console.WriteLine("addition of number is="+res);
                Console.ReadLine();

            }
        }

       public static void Main(string[] args)
        {
            student obj = new student();
            del delobj = new del(obj.add);
            delobj(14,15);   

        }
    }
     
}

OUTPUT:-

Description:- In this above program one delegate Object (delobj) is calling only one method(show()) of the student class.So this is called single cast delegate.

2. Multicast delegate:- One delegate object can call more than one Method in Sequence of any class.
In case of Multicast delegate,Delegate should not have any return type because after calling the delegate object we will get the return value only  by last method.
Ex:-

using System;
using System.Text;
namespace multidelegate
{
    class Program
    {
        public static int res3;
        public delegate void del(int p,int q);
        public delegate int del1(int x);
        public class student
        {
            public void add(int a,int  b)
            {
                int res1 = a + b;
                Console.WriteLine("addition of number is=" +res1);
                Console.ReadLine();

            }
            public void multi(int m,int n)
            {
                float res2 = (m * n);
                Console.WriteLine("Multiplication of number is=" +res2);
                Console.ReadLine();

            }
            public int saquare(int r)

            {
                 res3 = r * r;
                return res3;
               
            }

        }
     public static void Main(string[] args)
        {
            student st = new student();
            del obj1 = new del(st.add);
            obj1 += new del(st.multi);
            obj1(12,12);


            del1 obj2 = new del1(st.saquare);
            obj2(5);
            Console.WriteLine("squre of number is=" +res3);
            Console.ReadLine();

        }
    }
}
OUTPUT:

Description:- In this above example one delegate object(obj1is calling two method[add(),multi()] of student class,and another delegate object(obj2) is calling one method (saquare).so this is example of single and multicast delegate.
Real Definition of delegate in Our life:-
Suppose there are three person in a department.
  1. Teacher
  2. Peon
  3. Tea Maker
In one story,Teacher Ring the well,a peon came.Teacher said for tea,peon went to Tea maker and ordered a tea.Tea Maker accepted the order and made the tea.Peon came back with tea.
Now conclusion of this story is favor of Delegate is;
here:-
Teacher-->Event Creator
Ring --> Event
Peon --> Delegate
Tea maker-->Event handler
Anonymous method in context of delegate:- 
An anonymous is a collection of method which is directly pass to the delegate.
                                        OR
Anonymous is a method which does not have any specific name but it is a collection of statement which is directly pass to the delegate.
EX.

using System;
using System.Text;
namespace anonymous
{
    class Program
    {
        public delegate int del(String s);
        static void Main(string[] args)
        {
            del delobj = delegate(String s)
            {
                
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(i);
                    Console.WriteLine(s);
                    Console.ReadLine();
                }
                return 1;
            };
           int res= delobj("customer");
           Console.WriteLine(res);
           Console.ReadLine();
        }
    }
}
OUTPUT:

Description:-In above example i have directly passed statement to the delegate.So this is called Anonymous method.
Note:-
  1. We can declare the delegate outside or inside the class.
  2. But we can declare the Event only inside the class.
  3. Every event has a specific delegate for handling the event.
For More:-
I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application.
       Download

1 comment:

Powered by Blogger.