Delegate and event in C#

1
Delegate in C#:-


It is called a function pointer means we can point to the address of a function using a delegate to call the actual function.

If we do not want to call a particular function in the program then we can use the delegate concepts to call that function indirectly.

means we can say that delegate is an intermediate between function and object.

Syntax of Delegate:-

public delegate returntype  delegatename();

Type of Delegate:-

1) Single Cast  Delegate:-  We can call one delegate to one function

class DelegateExample
    {
        public void fun()
        {
            Console.WriteLine("Fun");
        }
        public delegate void mydelegate();
        static void Main()
        {
            DelegateExample del = new DelegateExample();
            mydelegate obj = new mydelegate(del.fun);
            obj();
            Console.ReadKey();
        }
    }
2) Multicast Delegate:-  We can \point one delegate to multiple functions address

public delegate void delegatename(param);
class DelegateExample
    {
        public void fun()
        {
            Console.WriteLine("Fun");
        }
        public void fun1()
        {
            Console.WriteLine("Fun1");
        }
        public void fun2()
        {
            Console.WriteLine("Fun2");
        }
        public delegate void mydelegate();
        static void Main()
        {
            DelegateExample del = new DelegateExample();
            mydelegate obj = new mydelegate(del.fun);
            obj += del.fun1;
            obj += del.fun2;
            obj();
            Console.ReadKey();
        }
    }


Another Example of Delegate in C#:-

class DelegateExample
    {
        public void fun1()
        {
            Console.WriteLine("Delegate Example");
        }
        public void fun2()
        {
            Console.WriteLine("Delegate Example2");
        }
       public delegate void mydelegate();
       static void Main()
       {
           DelegateExample obj = new DelegateExample();
           mydelegate del = new mydelegate(obj.fun1);
           del += obj.fun2;  //del = del + obj.fun2
           del -= obj.fun1;
           del();
           Console.ReadLine();

       }


    }

Multicast Delegate Example in C#:-

 class DelegateExample
    {
        public delegate void Mydelegate(int a,int b);
        public void fun(int a, int b)
        {
            Console.WriteLine(a+b);
        }
        public void fun1(int a, int b)
        {
            Console.WriteLine(a-b);
        }
        public void fun2(int a, int b)
        {
            Console.WriteLine(a*b);
        }
        public static void Main()
        {
            DelegateExample obj = new DelegateExample();
            Mydelegate del = new Mydelegate(obj.fun);
            del += obj.fun1;
            del += obj.fun2;
            del(10,2);
            del -= obj.fun2;
            del(100, 20);
          
            Console.ReadKey();
        }

    }


Event in c#:

it is a notification send by an object to singnal the ocuurence of an action, event contains two classes first is publisher and another one is subscriber. publisher publish the message and it will be received by subscriber class.

Delegate is the part of the event and it point to method.

Event is mainly used into GUI programming for example if we click on button then click is the event.

Now i am providing simple example of event.

Create Publisher Class.

internal class EventDemo
    {
        public delegate void EventHandler(); //create delegate
        public event EventHandler MyEvent // create event with add and remove event
        {
            add
            {
                Console.WriteLine("add operation");
            }
            remove
            {
                Console.WriteLine("remove operation");
            }
        }

    }
}


using System.Collections;
using System.Collections.Generic;

namespace Csharpfundamental
{
    internal class Program
    {
        public void TestEvent()
        {
            EventDemo myTest = new EventDemo();
            myTest.MyEvent += MyTest_MyEvent; // add event
            myTest.MyEvent -= MyTest_MyEvent; //remove event
        }

        private void MyTest_MyEvent()
        {
            
        }

        public void myTest_MyEvent(object sender, EventArgs e)
        {
        }
        public static void Main()
        {
            Program obj = new Program();
            obj.TestEvent();
            
        }

       
    }
}
...........................................................


Tags

Post a Comment

1Comments

POST Answer of Questions and ASK to Doubt

  1. Another example of delegate

    class DelegateExampleNew
    {
    void fun1(int a,int b)
    {
    Console.WriteLine(a + b);
    }
    void fun2(int a, int b)
    {
    Console.WriteLine(a - b);
    }
    public delegate void mydel(int a, int b);

    public static void Main()
    {
    DelegateExampleNew obj = new DelegateExampleNew();
    mydel o = new mydel(obj.fun1);
    o += obj.fun2;
    o(10, 20);
    o(100, 200);
    Console.ReadKey();
    }





    }

    ReplyDelete
Post a Comment