Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dnasca/94c6942e5ee6df28cfbc to your computer and use it in GitHub Desktop.
Save dnasca/94c6942e5ee6df28cfbc to your computer and use it in GitHub Desktop.
Default and Explicit Interface Implementations
using System;
/* //Default and Explicit Interface Implementation//
*
* If you want to make one of the interface methods act as default -- then implement that method normally, and the other interface explicitly.
* + This will make the default method able to be accessed through a class instance
*
*/
interface I1 //default interface method implement implicitly
{
void InterfaceMethod();
}
interface I2 //not default, implement explicitely
{
void InterfaceMethod();
}
public class Program : I1, I2
{
public void InterfaceMethod() //implicit interface member implementation
{
Console.WriteLine("I1 interface InterfaceMethod called");
}
void I2.InterfaceMethod() //explicit interface member implementation
{
Console.WriteLine("I2 interface InterfaceMethod called");
}
public static void Main()
{
var P = new Program();
P.InterfaceMethod(); //this will invoke the default, implicit, interface implementation
((I2)P).InterfaceMethod(); //this will invoke the explicit interface implementation (typecast P to be of type I2 for access)
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment