C# Program to Declare an Event Handler in an Interface
Posted by Superadmin on August 12 2022 06:19:24

C# Program to Declare an Event Handler in an Interface

 

 

This is a C# Program to illustrate handling an event declared in an interface.

Problem Description

This C# Program Illustrates Handling an Event Declared in an Interface.

Problem Solution

Here an event is handled with an interface.

Program/Source Code

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();
        }
    }
}
Program Explanation

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.

Runtime Test Cases
 
ClassA receives the Square event.
Drawing a shape.
ClassB receives the Rectangle event.
Press any key to exit.