Event In C#

By
Event is a kind of Notification(Information) giving one by one object to perform task. Event handling is not possible without delegate because it is delegate who will be responsible to call Event handler which even is fired.
Event Declaration:-
<access specifier-->Event Keyword-->Delegate Name--> Event Name>;
Ex:-

using System;
namespace Eventhandling
{
    class Program
    {
        public delegate void del();

        public class employee
        {
            public event del MyEvent;
            public void fire()
            {
                if (MyEvent != null)
                {

                    MyEvent();
                   
                }
                else
                {
                    Console.WriteLine("Event not handaled");
                    Console.ReadLine();
                }
            }
        }
        static void Main(string[] args)
        {
            employee emp = new employee();
            emp.MyEvent += new del(emp_MyEvent);//after += use tab in your visual studio
            emp.fire();
        }

        static void emp_MyEvent()
        {
            Console.WriteLine("Event handaled");
            Console.ReadLine();
        }

        }
    }   

OUTPUT:-

Note:- After += Use Tab two times in your keyboard .
For More:-

  1. oops concepts
  2. Constructor and Destructor in c#
  3. Multiple Main method in C#
  4. Abstract class and Abstract method
  5. Basic elements for compiling the C# code
  6. Collections in C#

I hope this is helpful for you.
Click below for download whole application
       Download

2 comments:

  1. Sir, what could be the real time example of above title... ?

    ReplyDelete
  2. Hi Sandeep Sharma !
    Suppose Ram and Shayam are two persons,They quarrel each other everyday,they generate event everyday .suppose ram has beaten by shayam one day,this is called another event.so you can say event is an task,which can be done by human,machine or others. After this event police came ram and shayam's house ,This police is called Event Handler.
    This police came on behalf of senior police officer.This senior police is called Delegate which give the order to police to do some task.
    Every event is a name that is called case ,in police term Ex. 302,305,420 etc.
    Every police officer can handle only your area event,because every police station has your own area they active only those area.Here Area Limitation is called Access Specifier.




    ReplyDelete

Powered by Blogger.