C# EventHandler

PHPz
Release: 2024-09-03 15:28:56
Original
1310 people have browsed it

An EventHandler in C# Programming Language is responsible for dealing with the events, which are programmed by the coder, to be executed when intended, asynchronously. Every programming language has its functions and limitations, and the Event handler is one of the great functions for the proper execution of the program.

We understand any event that occurs is an action, which is a result of another action, like a simple click button followed by the functions. Delegate is an important part of the eventhandler, and when created, it aims towards the method eventhandler.

Syntax:

Now that we have understood what the eventhandler is, let us move on to learn more about it. The syntax for a function or a method is a crucial part, and a simple syntax for Event handler method is as follows:

public delegate void SimpleEH(int a, int b);
Copy after login
  • You must have noticed the delegate keyword, which is of a special type and purely represent the methods. And the two arguments that we have are the object and the EventArgs, which might have different purposes.
  • This simple delegate above has a basic operation of pointing towards the method of event handling that accepts two parameters of an integer and also returns an integer. This syntax for the method can be declared at the level of the namespace, which will implement a simple rule that there is no need to repeat it in any nested classes.

How EventHandler works in C#?

We have nicely learned what the eventhandler in C# is and its respective syntax. But understanding the working of the eventhandler is an important part, which helps in better implementation. Every move or step in a program is an event, which is handled by an eventhandler. We have a method for eventhandler, and the delegate is used to point towards that method. Here the delegate can be of anyone type out of these five: class, interface, structure, enumeration, and a delegate.

We have to create an instance of the delegate that we have already learned with syntax. The delegate we create is pointing toward the eventhandler method.  Here, we must remember that all the C# events in .NET are basically based on delegates.

Basically, we have to define an event handler method within the event receiver in order to respond to an event. In order for better implementation, the signature with the delegate representing the event must match the method for the event that we’re at present handling.

Examples to Implement C# EventHandler

Below are the examples mentioned:

Example #1

We have understood the eventhandler method, its syntax, along with its working. Now we move on to implementation; here, we will write a program to print the edition and execute it.

Code:

using System;
public delegate int EHsample(int a, int b);
class Program {
static void Main() {
Adder a = new Adder();
EHsample instanceEHsample = new EHsample(a.Add);
int sampleOutput = instanceEHsample(4, 3);
Console.WriteLine("\n sampleOutput = {0}", sampleOutput);
}
}
public class Adder {
public int Add(int x, int y)
{ return x + y; }
}
Copy after login

Output:

C# EventHandler

Explanation: We simply began with importing our system. Followed by a declaration of a delegate. We have already understood the syntax which we are implementing here. We have two arguments without delegate, both of integer, a and b. Then our class Program, with the main method. We have a simple Adder, with a new instance. We have created an Adder class further in the program. Then we have our instance for the delegate created and called our adder instance to add. We then simply passed the two values, which here are 4 and 3. Finally, we have our print statement, which will print sampleOutput =, followed by the addition of the two values we passed.

Then we have our public class Adder, where the operation of adding takes place for the values we passed earlier. Add function takes in two arguments and returns the addition of both, and passes it to the output. For proper output, refer to the below screenshot:

Example #2

Moving on, we will implement the eventhandler delegate method with our next example.

Code:

using System;
public delegate void sampleEventHandler();
class Program {
public static event sampleEventHandler _show;
static void Main() {
_show += new sampleEventHandler(Event);
_show += new sampleEventHandler(Handler);
_show.Invoke();
}
static void Event() {
Console.WriteLine("\n Event");
}
static void Handler() {
Console.WriteLine("\n Handler");
}
}
Copy after login

Output:

C# EventHandler

Explanation: Similar to our first example, we have used a statement, then our declaration for the delegate and the class with the main method. We have shown method instances where we add new events to the list. Then we add two events: Event and Handler. For the purpose of simplicity, we have used the static modifier for the event, which will allow direct access to the event within the static Main method.

Also, the += used here is of no connection with the arithmetic operations. Refer to the below screenshot for the output:

Conclusion

Every event is an action, and the eventhandler properly handles that event. We create an instance for the delegate and call it when required; the delegate instance points towards the eventhandler method. These events are widely used in the Windows Forms Framework and are the eventhandler, and in case of threading, we implement the BackGroundWorker type.

The above is the detailed content of C# EventHandler. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!