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