Use explicit implementation to remove ambiguity : Interface « Class Interface « C# / C Sharp






Use explicit implementation to remove ambiguity

Use explicit implementation to remove ambiguity
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use explicit implementation to remove ambiguity. 
 
using System; 
 
interface IMyIF_A { 
  int meth(int x); 
} 
 
interface IMyIF_B { 
  int meth(int x); 
} 
 
// MyClass implements both interfaces. 
class MyClass : IMyIF_A, IMyIF_B { 
 
  // explicitly implement the two meth()s 
  int IMyIF_A.meth(int x) { 
    return x + x; 
  } 
  int IMyIF_B.meth(int x) { 
    return x * x; 
  } 
 
  // call meth() through an interface reference. 
  public int methA(int x){ 
    IMyIF_A a_ob; 
    a_ob = this; 
    return a_ob.meth(x); // calls IMyIF_A 
  } 
 
  public int methB(int x){ 
    IMyIF_B b_ob; 
    b_ob = this; 
    return b_ob.meth(x); // calls IMyIF_B 
  } 
} 
 
public class FQIFNames { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
 
    Console.Write("Calling IMyIF_A.meth(): "); 
    Console.WriteLine(ob.methA(3)); 
 
    Console.Write("Calling IMyIF_B.meth(): "); 
    Console.WriteLine(ob.methB(3)); 
  } 
}

           
       








Related examples in the same category

1.Interface with method name conflicts
2.Demonstrate the ByTwos interfaceDemonstrate the ByTwos interface
3.Demonstrate the ByTwos interface 2Demonstrate the ByTwos interface 2
4.Use a property in an interfaceUse a property in an interface
5.Add an indexer in an interfaceAdd an indexer in an interface
6.One interface can inherit anotherOne interface can inherit another
7.Explicitly implement an interface memberExplicitly implement an interface member
8.Two class inherit one interfaceTwo class inherit one interface
9.Using interface 3Using interface 3
10.illustrates interfacesillustrates interfaces
11.illustrates implementing multiple interfacesillustrates implementing multiple interfaces
12.illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
13.illustrates casting an object to an interfaceillustrates casting an object to an interface
14.illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
15.illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
16.illustrates an explicit interface member implementationillustrates an explicit interface member implementation
17.illustrates interface member hidingillustrates interface member hiding
18.Demonstrates the use of a simple interfaceDemonstrates the use of a simple interface
19.Interface demoInterface demo
20.Interface demo: implements two interfacesInterface demo: implements two interfaces
21.Interface castingInterface casting
22.Overriding InterfacesOverriding Interfaces
23.Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
24.A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
25.Interfaces:Working with InterfacesInterfaces:Working with Interfaces
26.Reimplementation of Interfaces
27.Interface with event