illustrates an explicit interface member implementation : Interface « Class Interface « C# / C Sharp






illustrates an explicit interface member implementation

illustrates an explicit interface member implementation
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example8_7.cs illustrates an explicit interface member
  implementation
*/

using System;


// define the IDrivable interface
public interface IDrivable
{
  void TurnLeft();
}


// define the ISteerable interface
public interface ISteerable
{
  void TurnLeft();
}

// Car class implements the IMovable interface
class Car : IDrivable, ISteerable
{

  // explicitly implement the TurnLeft() method of the IDrivable interface
  void IDrivable.TurnLeft()
  {
    Console.WriteLine("IDrivable implementation of TurnLeft()");
  }

  // implement the TurnLeft() method of the ISteerable interface
  public void TurnLeft()
  {
    Console.WriteLine("ISteerable implementation of TurnLeft()");
  }

}


public class Example8_7
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car();

    // call myCar.TurnLeft()
    Console.WriteLine("Calling myCar.TurnLeft()");
    myCar.TurnLeft();

    // cast myCar to IDrivable
    IDrivable myDrivable = myCar as IDrivable;
    Console.WriteLine("Calling myDrivable.TurnLeft()");
    myDrivable.TurnLeft();

    // cast myCar to ISteerable
    ISteerable mySteerable = myCar as ISteerable;
    Console.WriteLine("Calling mySteerable.TurnLeft()");
    mySteerable.TurnLeft();

  }

}

           
       








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.Use explicit implementation to remove ambiguityUse explicit implementation to remove ambiguity
9.Two class inherit one interfaceTwo class inherit one interface
10.Using interface 3Using interface 3
11.illustrates interfacesillustrates interfaces
12.illustrates implementing multiple interfacesillustrates implementing multiple interfaces
13.illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
14.illustrates casting an object to an interfaceillustrates casting an object to an interface
15.illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
16.illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
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