Explicit Interface Implementation - CSharp Custom Type

CSharp examples for Custom Type:interface

Introduction

Implementing multiple interfaces can sometimes result in a collision between member signatures.

You can resolve such collisions by explicitly implementing an interface member.

Consider the following example:

interface I1 { void Foo(); }
interface I2 { int Foo(); }

public class Widget : I1, I2
{
  public void Foo()
  {
    Console.WriteLine ("Widget's implementation of I1.Foo");
  }

  int I2.Foo()
  {
    Console.WriteLine ("Widget's implementation of I2.Foo");
    return 42;
  }
}
   
Widget w = new Widget();
w.Foo();                      // Widget's implementation of I1.Foo
((I1)w).Foo();                // Widget's implementation of I1.Foo
((I2)w).Foo();                // Widget's implementation of I2.Foo

Related Tutorials