HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Interfaces in C# : Part 2
Implementing 2 or more interfaces that have the same method name.
In the example below, both IInterface1 and IInterface2 have the SameMethod() name. MainClass implements both IInterface1 and IInterface2. In the MainClass the way we implement IInterface1 SameMethod() and IInterface2 SameMethod() is a little different.

We precede the SameMethod() by the name of the interface containing that method. This is to avoid the naming ambiguity of which interface SameMethod() we are implementing.

In the Main() method, we typecast the MainClass object to the appropriate interface type and then invoke the SameMethod() as shown below.
  1. ((IInterface1)MC).SameMethod();  invokes IInterface1 SameMethod() Method.
  2. ((IInterface2)MC).SameMethod();  invokes IInterface2 SameMethod() Method.

using System;

interface IInterface1

{

    void SameMethod();

}

interface IInterface2

{

    void SameMethod();

}

class MainClass : IInterface1,IInterface2

{

    void IInterface1.SameMethod()

    {

        Console.WriteLine("Interface1SameMethod Implemented");

    }

    void IInterface2.SameMethod()

    {

        Console.WriteLine("Interface2SameMethod Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        ((IInterface1)MC).SameMethod();

        ((IInterface2)MC).SameMethod();

    }

}

Using "is" operator to determine if a class has implemented an interface
We can use "is" operator to progrmatically determine if a class has implemented an interface. Example below demonstrates the usage of "is" operator.
  1. We have 2 interfaces - IInterface1,IInterface2
  2. MainClass implements only IInterface1 interface.
  3. MC is an instance of the MainClass. We are checking if MC is of IInterface1. The statement below would return true, if MainClass has implemented IInterface1 else it will return false.
          if (MC is IInterface1)

using System;

interface IInterface1

{

    void IInterface1Method();

}

interface IInterface2

{

    void IInterface2Method();

}

class MainClass : IInterface1

{

    public void IInterface1Method()

    {

        Console.WriteLine("IInterface1Method Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        if (MC is IInterface1)

        {

            Console.WriteLine("MainClass Implemented IInterface1");

        }

        else

        {

            Console.WriteLine("MainClass did not Implemented IInterface1");

        }

    }

}
Using "as" operator to determine if a class has implemented an interface
We can use "as" operator also, to progrmatically determine if a class has implemented an interface. Example below demonstrates the usage of "as" operator.

Difference between "is" and "as" operator
  • "is" operator returns true or false.
  • "as" operator returns null or an instance.
The above program has been rewritten using the "as" operator.

using System;

interface IInterface1

{

    void IInterface1Method();

}

interface IInterface2

{

    void IInterface2Method();

}

class MainClass : IInterface1

{

    public void IInterface1Method()

    {

        Console.WriteLine("IInterface1Method Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        if (MC as IInterface1 != null)

        {

            Console.WriteLine("MainClass Implemented IInterface1");

        }

        else

        {

            Console.WriteLine("MainClass did not Implemented IInterface1");

        }

    }

}

Further Reading
Other related articles

Njoy Programming
   ByPrasad Cherukuri