Events must be responded as soon as possible by applications. Such that Interrupts, Inter-process communication is accomplished through the usage of events. User actions, such as keystrokes, clicks, and mouse movements such as system-generated events, are both examples of user actions. In C#, an event is a delegate that has been encapsulated. The delegate defines the method declaration of the event handler for the class subscriber. In the article, we will specify the Event that occurs in C# and also notifies the other classes about it, as well as discuss the occurrence event handler in C#.

What is Event in C# in Ubuntu 20.04?

Delegates are used to define events in C#, which are a subset of delegates. A publisher is required to raise an event in C#, and a subscriber class is also required for receiving and handling an event. The event is published with the help of the publisher class. The subscriber class is an additional class that processes such events. A publisher-subscriber paradigm is used for events in C#. In general, the class that sends the event is known as the publisher class, and the class that receives the event is known as the subscriber class.

Syntax of Declaring the Event in C# in Ubuntu 20.04

A function or method’s syntax is important in programming, and we have simple syntax for the event handler function is as follows:

Public delegate void DelegateHandler (); // delegate

First, the delegate’s event type must be specified before the event can be declared in the class.

Public event void DelegateHandler EventHandler (); // event

The preceding code creates a “DelegateHandler” by using the keyword delegate and an “EventHandler” event that calls the delegate when it is raised. Note that only in the class where the Event was declared can be used to invoke it.

How to Declare the Events in C# in Ubuntu 20.04

To understand how the delegate event and event handler works in C#, we should have hands-on programming examples. These examples are implemented in Ubuntu 20.04 by using a mono compiler for C#.

Example # 1: Program of Using Delegates in C# in Ubuntu 20.04

The example is simply declaring the delegate and also declaring a delegate variable with the event keyword. We have declared an event for the following code in the publisher class.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-1.jpg" data-lazy- height="406" src="data:image/svg xml,” width=”676″>

We have imported the system library of C# as “using the system” which helps us to include the classes and functions such as the Console class and the WriteLine function. The namespace is given the name “MyApp” which identifies and encapsulates our code. The delegate is defined here as “Del” of type string and passes “str” of string as a parameter. After that, the publisher class is created as “EventExample”. In the publisher class, we have declared a delegate “Del” and using the “event” modifier, we have declared an event for the type delegate “Del”.Then, we have called “EventExample” and created an object “this.Event”.

The object is initialized with the delegate “Del” by using a new keyword and passing another object “this.HelloUser” to it. We have called “HelloUser” as a string type which has passed with a new object “MyUser” as a parameter. The return statement will return the value of MyUser along with the string “Hello”. The void main has invoked the publisher class “EventExample” where we have assigned a value to “MyUser” in the “obj. Event”. The “obj. Event” is initialized for the “Result” variable which we have passed in the console.WriteLine method for display.

The output of the above program execution is displayed on the console screen.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-1.jpg" data-lazy- height="142" src="data:image/svg xml,” width=”715″>

Example # 2: Program of Consuming an Event in C# in Ubuntu 20.04

The subscriber class must register for the event and handle it with the method whose declaration matches with the delegate as shown in the below program.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-1.jpg" data-lazy- height="506" src="data:image/svg xml,” width=”656″>

The event “MyProcessCompleted” is subscribed to the class “Myprogram”. It uses the operator “ =” to register with the event. Remember that we can add methods to the invoking list of the multicast delegate in the same way. The event is handled here by the“p1_MyProcessCompleted ()” method, which matches the delegate “Notifier” type declaration and the console. Writeline method is invoked to print the particular message. The OnProcessCompleted() method is called at the end of the StartProcess() method, which raises an event.

Then, the event name “OnProcessCompleted” is called and defined with the protected virtual method. The OnProcessCompleted() method uses ProcessCompleted. Invoke() to call the delegate. This verifies first that the event “MyProcessCompleted” should not be null and then, calls the delegate “Notifier”.

Upon execution of the overhead code, we have the output printed on the screen appended below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-1.jpg" data-lazy- height="161" src="data:image/svg xml,” width=”717″>

Example # 3: Program of Passing Data in Built-in EventHandlerDelegate in C# in Ubuntu 20.04

The majority of events send data to their subscribers. The EventArgs class serves as the foundation for all event data classes. To pass the data to the handler, we use EventHandler. The code is illustrated below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-1.jpg" data-lazy- height="582" src="data:image/svg xml,” width=”743″>

Here, we have a class called “EventHandlerprogram”. In the class, we have a void main function called which has an object “EventHandlerProcess” as P1. The object is initialized with the new keyword and invoked by the EventHandlerProcess(). The p1_ProcessCompleted is registered as an event here by using the operator “ =”. At the end of the void main, the StartProcess() function is also invoked for the OnProcessCompleted method that raises an event. Then, we have an event handler “P1_ProcessCompleted” in which we have passed an object as “Source” and type bool as “IsSuccessful”.

The consoleWriteLine will print the Boolean statement. The class” EventHandler” is created in which we have a built-in EventHandler for declaring an event. Also, we have invoked the StartProcess method which has a try-catch block for exception handling if occurs. The “OnProcessCompleted” is called with the protected virtual method. In the OnProcessCompleted() function, we use invoke() to raise an event, we have specified “this” as a sender and bool as “IsSuccessful”. Note that the handlers are given a single bool value that indicates whether the process succeeded or failed.

The output of the compilation of the code is shown in the below image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-1.jpg" data-lazy- height="159" src="data:image/svg xml,” width=”717″>

Example # 4: Program of Declaring and Raising Events in C# in Ubuntu 20.04

The following is an example in which we have to declare and raise an event in the C# programming language utilizing the core delegate type.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-1.jpg" data-lazy- height="596" src="data:image/svg xml,” width=”767″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image9-1.jpg" data-lazy- height="455" src="data:image/svg xml,” width=”719″>

In the above example, we have a class “ArithmeticOperations” as a publisher class in which we have performed the needed functionality that checks whether the invoking class subscribed to the event or not. The delegate is declared in the publisher class as a delegate and the event is declared as “MyEvent”. The void Add is taking “x1” and “y1” as parameter values. In the void Add block, we have called a delegate through the if-else statement for checking the subscription. The event “MyEvent” is raised in the if-else statement and prints the message through the WriteLine method. Then, we have the subscriber class as operations with the required methods. We used the = operator in the Operations class to subscribe to the “MyEvent” event and named a handler as “MyEventHandler” for performing the appropriate operations when an event was raised. The handler method “MyEventHandler” in the Operations class has the same implementation as in the delegate method “Delegate” in the “ArithmeticOperations” class.

You can see the output of the basic arithmetic operation by event handler in C# below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image10-1.jpg" data-lazy- height="209" src="data:image/svg xml,” width=”714″>

Conclusion

We have deeply gone through the C# event by discussing what the events are in C# and the basic syntax we implement in programming. We have also executed the example of events and event handlers by showing how to create simple events, raise an event and also subscribe to an event by showing their output on the Ubuntu terminal screen. These are very basic examples that might help you to understand clearly.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg6253a60f22d5e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.