Mega Code Archive

 
Categories / C# / Class Interface
 

Explicitly implement an interface member

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Explicitly implement an interface member.    using System;    interface IEven {    bool isOdd(int x);    bool isEven(int x);  }    class MyClass : IEven {    // explicit implementation    bool IEven.isOdd(int x) {      if((x%2) != 0) return true;      else return false;    }      // normal implementation    public bool isEven(int x) {      IEven o = this; // reference to invoking object        return !o.isOdd(x);    }  }    public class Demo {    public static void Main() {      MyClass ob = new MyClass();      bool result;        result = ob.isEven(4);      if(result) Console.WriteLine("4 is even.");      else Console.WriteLine("3 is odd.");        // result = ob.isOdd(); // Error, not exposed    }  }