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);
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.
Below are the examples mentioned:
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; } }
Output:
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:
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"); } }
Output:
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:
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!