Casting an interface to another interface that it does not inherit

The actual type of the instance first points to implements both interface. So obviously both Method1 and Method2 are available on the object.

The static type of first only lets you access Method1. The static type of second only lets you access Method2. I you declare a reference to the object using either of the interfaces, you just select to view the instance as an object fulfilling the selected contract (the interface).

As InterfaceImplementation implements both interfaces, you have the option of referring to the instance using either of the interfaces.


From your example, you should be good by testing type type before calling any of the functionality. The first creation will create a fully qualified "InterfaceImplementation" that supports both interfaces. However, you are putting it into a declared type of only the first interface. So from the "first" object's perspective, it only cares about anything associated as an IFirstInterface implementation.

Now, on to you second... Even though you've created the object, you can still ask... By the way... are you also a Second Interface? If so, do this...

IFirstInterface first = new InterfaceImplementation();

if( first is ISecondInterface )
  // typecast since the second interface is legit, then call it's method 2
  ((ISecondInterface)first).Method2();