Free mag vol1 | Page 360

CHAPTER 8  WORKING WITH INTERFACES } } As you can see, when explicitly implementing an interface member, the general pattern breaks down to: returnType InterfaceName.MethodName(params){} Note that when using this syntax, you do not supply an access modifier; explicitly implemented members are automatically private. For example, the following is illegal syntax: // Error! No access modifer! public void IDrawToForm.Draw() { Console.WriteLine("Drawing to form..."); } Because explicitly implemented members are always implicitly private, these members are no longer available from the object level. In fact, if you were to apply the dot operator to an Octagon type, you would find that IntelliSense does not show you any of the Draw() members. As expected, you must make use of explicit casting to access the required functionality. For example: static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the "as" keyword. if(oct is IDrawToMemory) ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); While this syntax is quite helpful when you need to resolve name clashes, you can use explicit interface implementation simply to hide more “advanced” members from the object level. In this way, when the object user applies the dot operator, he or she will see only a subset of the type’s overall functionality. However, those who require the more advanced behaviors can extract out the desired interface via an explicit cast. 298