Created
March 17, 2015 06:25
-
-
Save dnasca/7ad11c21bbb7a1c91770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| /* Explicit Interface Implementation | |
| * | |
| * When you are explicitly implementing an interface member, you are not allowed to use an access modifier | |
| * | |
| * You have to use the interface name dot MethodName, ex. IInterface1.InterfaceMethod(); | |
| * | |
| * When a class explicitly implements an interface member, the interface member can no longer be accessed through a | |
| * class reference variable, but instead can only be accessed via the interface reference variable | |
| * | |
| */ | |
| interface I1 | |
| { | |
| void InterfaceMethod(); | |
| } | |
| interface I2 | |
| { | |
| void InterfaceMethod(); | |
| } | |
| public class Program : I1, I2 | |
| { | |
| void I1.InterfaceMethod() //explicit 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() | |
| { | |
| //invocation using typecasting | |
| var P = new Program(); | |
| ((I1)P).InterfaceMethod(); //access by typecasting object(P) reference variable to be of type I1 | |
| ((I2)P).InterfaceMethod(); //access by typecasting object(P) reference variable to be of type I2 | |
| Console.WriteLine("-------------------"); | |
| //invocation by creating an object reference variable of interface type | |
| I1 i1 = new Program(); | |
| I2 i2 = new Program(); | |
| i1.InterfaceMethod(); | |
| i2.InterfaceMethod(); | |
| //cannot invoke P.InterfaceMethod() when using explicit interface implementation | |
| Console.ReadKey(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment