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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.