Skip to main content

Delegate and event in C#

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();
            
        }

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


Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...