Topic : Events in C# Programming Language:

Events:

  • Events are Nothing, but just a user action. Such as key press, clicks, mouse Movements, etc. or some Occurrence such as system Generated Notifications. For Example:
    » When we click with mouse - it is a mouse click events.
    » When we press any key in keyboard - it is key press events.
    » When we Refresh our Webpage - is is pate load events.
    » When we move Mouse Cursor - it is mouse hover events.



Microsoft Launches events for Developers, to make them aware about the features of New or existing products. Microsoft Notifies the Developers about the Event by Email or other Advertisement Options. So in this case, Microsoft is a Publisher Who launches " raises " an Event and Notifies the Developers about it and developers are the Subscribers of the event and attend " Handle " the event.



 Events : infobrother

Events in C# follow the similar concept. An event has a Publisher, subscriber, notification and a Handler. Generally, UI controls use events extensively. For Example, the button control in a windows form has multiple events such as click, mouse-over etc. A custom Class can also have an event to Notify other Subscriber classes about something that has happened or is going to happen.



Using Delegates with Events:

The Events are declared and Raised in a class and Associated with the event handlers using Delegates within the Class. The class containing the event is used to publish the event. This is called the Publisher class. some other class that accepts this event is called the Subscriber class. Events Use the Publisher and Subscriber Model.




Publisher:

Publisher is an Object that contains the Definition of the Event and the Delegate. The Event-Delegate association is also defined in this Object. a Publisher class object invokes the event and it is notified to other objects.


Subscriber:

Subscriber is an object that accepts and provides an event handler. the Delegate in the publisher class invokes the Method ( Event Handler ) of the subscriber class.



Events Declaration:

An Event is Nothing but an Encapsulated Delegates . As we have learned in Our Previous Tutorial, a delegate is a reference type data type. so to declare an event inside a class, first we need to declare an Delegate type for the event, as show below:


//Declare an Delegate type for the Event:
Access_Modifier  delegate Return_Type  delegate-name( Parameters)

After Declaring an Delegate type, we can declare an event using event keyword as shown below.


//Declare an Event using delegate:
Access_Modifier  event   delegate-name   Event_Name;

  • In Above Syntax:

    » Access_Modifier is the access scope of event.
    » event is the keyword, use to declare an event.
    » delegate_name is the name of delegate type, that has been declare for this event.
    » Event_Name is the name of this event. it could be any name same like variable but using the naming rules.



Let's have an simple example to understand how we declare and use event in C#.



/*Example - Events - InfoBrother*/

using System;

namespace EventHandler
{
    //Delegate declare:
    public delegate string Message(string msg);

    class Program
    {
        //declare Event of type Delegate:
        public event Message NewMessage;


        public Program() //constructor:
        {
            //subscribe to NewMessage event:
            this.NewMessage += new Message(this.readMsg);
        }

         //Event Handler:
        public string readMsg(string from)
        {
            return "You Got a New Message from " + from;
        }

        static void Main(string[] args)
        {
            Program obj = new Program(); //Object:
            string showMessage = obj.NewMessage("InfoBrother."); //calling method:
            Console.WriteLine(showMessage);

            Console.ReadKey();
        }
    }
}


  • More about Events:

    » Use event keyword with delegate type to declare an event.
    » Check event is null or not before raising an event.
    » Subscribe to events using "+=" operator. Unsubscribe it using "-=" operator.
    » Function that handles the event is called event handler. Event handler must have same signature as declared by event delegate.
    » Events can have arguments which will be passed to handler function.
    » Events can also be declared static, virtual, sealed and abstract.
    » An Interface can include event as a member.
    » Events will not be raised if there is no subscriber
    » Event handlers are invoked synchronously if there are multiple subscribers.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement