Events In C#

The occurrence of an Event holds significance as it represents a forthcoming happening. Let's consider an instance of an event where Microsoft organizes events targeted at developers. During this Event, Microsoft aims to inform developers about the functionalities of either current or new products. To achieve this, Microsoft will utilize Email and other advertising avenues to notify developers about the Event. Consequently, Microsoft functions as the organizer who initiates the Event and informs developers about it, while developers act as subscribers who participate in and manage the Event.

Similarly, within C#, the concept of Events is consistent. Within C#, an Event can function as a subscriber, publisher, notifier, and a handler. Typically, events are utilized within User Interfaces. Let's consider the example of a Button control in Windows. The Button triggers various events like click, mouseover, etc. Within a custom class, an Event is incorporated to alert other subscriber classes about impending actions. Therefore, in this scenario, we will establish the Event and notify other classes regarding the Event, encompassing the event handler.

The event represents a wrapped delegate. C# and .NET both endorse events utilizing delegates. Events and delegates notify the client application when the application's state changes. Event handling necessitates delegate implementation as both are closely linked in event dispatching. The class sending the event is referred to as the publisher, while the class receiving or handling the event is known as a subscriber.

Key Points about the Events are:

The key points about the events are as:

  • In C#, event handler will take the two parameters as input and return the void.
  • The first parameter of the Event is also known as the source, which will publish the object.
  • The publisher will decide when we have to raise the Event, and the subscriber will determine what response we have to give.
  • Event can contain many subscribers.
  • Generally, we used the Event for the single user action like clicking on the button.
  • If the Event includes the multiple subscribers, then synchronously event handler invoked.
  • Declaration of the Event

    Syntax

public event EventHandler CellEvent;

Steps for implementing the Event

To declare an Event within a class, the first step is to declare the delegate type for the event.

A delegate named CellEventHandler is declared with a void return type and parameters for the sender object and EventArgs e.

Declaration of the Event

public event CellEventHandler CellEvent;

Invokation of the Event

if (CellEvent != null) CellEvent(this, e);

We are only able to trigger the Event from within the class where the Event was initially declared.

Hooking up the Event

OurEventClass.OurEvent += new ChangedEventHandler(OurEventChanged);

Detach the Event

OurEventClass.OurEvent -= new ChangedEventHandler(OurEventChanged);

Delegates function as references to functions. They are a type of data that stores the address of a method. The System.Delegate class serves as the base class for all delegates.

Delegates are defined by using the delegate keyword, followed by its signature.

Syntax of Delegates

Example

<access modifier> delegate <return type> <delegate_name>(<parameters>)

Example of Delegates

public delegate void PrintWord(int value);

The PrintWord delegate mentioned above can be employed to reference any method that shares the identical return type and specified parameters as PrintWord. Below is an illustration demonstrating the declaration and utilization of the PrintWord delegates.

Example

class Program1
{
    // declare delegate
    public delegate void PrintWord(int value);

    static void Main(string[] args)
    {
        // Print delegate points to PrintNum
        PrintWord printDel = PrintNum;
        
        // or
        // Print printDel = new Print(PrintNumber);
            
        printDel(100000);
        printDel(200);

        // Print delegate points to PrintMoney
        printDel = PrintMoney;

        printDel(10000);
        printDel(200);
    }

    public static void PrintNum(int num)
    {
        Console.WriteLine("Number: {0,-12:N0}",num);
    }

    public static void PrintMoney(int money)
    {
        Console.WriteLine("Money: {0:C}", money);
    }
}

Output:

The given code snippet displays a placeholder diagram styled with a gradient background, border radius, padding, margin, and center-aligned text. The placeholder icon within the diagram is set to a font size of 3rem with a margin-bottom of 10px. The placeholder text is styled with a color of #9ca3af and a font size of 1rem.

In the example provided, we defined the PrintWord delegates, which take an integer type parameter and give back void. Within the main function, we specify the PrintWord type function and set it as the PrintNum method. Next, we will call the PrintWord delegate, which will then call the PrintNum method. Similarly, if the PrintWord delegates variable is set to the PrintMoney method, it will trigger the execution of the PrintMoney method.

Additionally, the delegate object can be instantiated using the new operator while specifying the method's name, as demonstrated below:

Example

PrintWord printDel = new PrintWord(PrintNum);

Delegates can be declared, as shown below:

Example

public delegate void someEvent();
public organize event

Input Required

This code uses input(). Please provide values below: