ClassA receives the Square event. Drawing a shape. ClassB receives the Rectangle event. Press any key to exit.
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Declare an Event Handler in an Interface
C# Program to Declare an Event Handler in an Interface
This is a C# Program to illustrate handling an event declared in an interface.
This C# Program Illustrates Handling an Event Declared in an Interface.
Here an event is handled with an interface.
Here is source code of the C# Program to IIlustrate Handling an Event Declared in an Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to IIlustrate Handling an Event Declared in an Interface */ namespace interfaceevents { using System; public interface square { event EventHandler Draw; } public interface rectangle { event EventHandler Draw; } public class Shape : square, rectangle { event EventHandler DrawEvent1; event EventHandler DrawEvent2; object objectLock = new Object(); event EventHandler square.Draw { add { lock (objectLock) { DrawEvent1 += value; } } remove { lock (objectLock) { DrawEvent1 -= value; } } } event EventHandler rectangle.Draw { add { DrawEvent2 += value; } remove { DrawEvent2 -= value; } } public void Draw() { EventHandler handler = DrawEvent1; if (handler != null) { handler(this, new EventArgs()); } Console.WriteLine("Drawing a shape."); handler = DrawEvent2; if (handler != null) { handler(this, new EventArgs()); } } } public class classA { // References the shape object as an square public classA(Shape shape) { square d = (square)shape; d.Draw += new EventHandler(d_Draw); } void d_Draw(object sender, EventArgs e) { Console.WriteLine("ClassA receives the square event."); } } public class classB { public classB(Shape shape) { rectangle d = (rectangle)shape; d.Draw += new EventHandler(d_Draw); } void d_Draw(object sender, EventArgs e) { Console.WriteLine("ClassB receives the rectangle event."); } } public class Program { static void Main(string[] args) { Shape shape = new Shape(); classA sub = new classA(shape); classB sub2 = new classB(shape); shape.Draw(); System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } }
This C# program is used to illustrate handling an event declared in an interface. Using classA and classB create the reference for the shape object as a square and Rectangle respectively.
Perform the shape procedure for square and rectangle, the lock statement is used to block a critical section by obtaining the mutual-exclusion lock for a given object, execute the statement, and then release the lock.
If condition statement is used for draw procedure to check the condition that handler variable value is not equal to null. If the condition is true, then execute the statement and perform the handler() method. Here an event is handled with an interface.