Implementing multiple interfaces that have the same method name and signature.

  1. How to implement the interface methods that have the same method name and signature.
  2. How to get access to the interface methods that have the same method name and signature.

Example 1:

    interface I1

    {

        void M();

    }

 

    interface I2

    {

        void M();

    }

 

    class TestMultipleInterfaces : I1, I2

    {

        void I1.M()

        {

            Console.WriteLine("I1");

        }

 

        void I2.M()

        {

            Console.WriteLine("I2");

        }

 

        public void M()

        {

            Console.WriteLine("TestMultipleInterfaces");

        }

    }

 

As we can see from the example 1 to implement both methods with the same name and signature we must prefix method name by interface name.

 

Example 2:

    class Program

    {

        static void Main(string[] args)

        {

            TestMultipleInterfaces obj = new TestMultipleInterfaces();

            obj.M();

            ((I1)obj).M();

            ((I2)obj).M();

            Console.ReadKey();

        }

    }

To get access to the interface methods that we must cast class instance to the interface.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s