How do I Implement Custom Event Handling in C#

Tne following source code shows how to implement custom event handling in C#.

using System;
class CustomEventBinding
{
public CustomEventBinding()
{
CustomEventHandling.operationEvent +=
new OperationEventHandler(ShowStatus);
SendNumber();
}
private void SendNumber()
{
CustomEventHandling customEventHandle =
new CustomEventHandling();
customEventHandle.CheckNumber(8);
customEventHandle.CheckNumber(9);
}
private void ShowStatus(string message)
{
Console.WriteLine(message);
}
}
class CustomEventHandling
{
public delegate void OpeartionEventHandler(string message);
public static event OperationEventHandler operationEvent;
int i = 0;
//Indicate whether given number is even or odd
public void CheckNumber(int number)
{
int rem = number % 2;
if(rem == 0)
operationEvent("Even Number");
else
operationEvent("Odd Number");
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all