/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// IntrFace.cs -- demonstrates the use of a simple interface
//
// Compile this program with the following command line:
// C:>csc IntrFace.cs
using System;
namespace nsInterface
{
interface IPlane
{
double Area
{
get;
}
}
interface ISolid
{
double Volume
{
get;
}
}
class clsCircle : IPlane
{
public clsCircle (double radius)
{
m_Radius = radius;
}
public double Area
{
get {return (3.14159 * m_Radius * m_Radius);}
}
private double m_Radius;
public override string ToString ()
{
return ("Area = " + Area);
}
}
class clsSphere : IPlane, ISolid
{
public clsSphere (double radius)
{
m_Radius = radius;
}
public double Area
{
get {return (4 * 3.14159 * m_Radius * m_Radius);}
}
public double Volume
{
get {return (4 * 3.14159 * m_Radius * m_Radius * m_Radius / 3);}
}
private double m_Radius;
public override string ToString ()
{
return ("Area = " + Area + ", " + "Volume = " + Volume);
}
}
public class IntrFace
{
static public void Main ()
{
clsCircle circle = new clsCircle (14.2);
clsSphere sphere = new clsSphere (16.8);
Console.WriteLine ("For the circle: " + circle);
Console.WriteLine ("For the sphere: " + sphere);
}
}
}
Month: February 2011
illustrates interface member hiding
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_8.cs illustrates interface member hiding
*/
using System;
// define the IDrivable interface
public interface IDrivable
{
void TurnLeft();
}
// define the ISteerable interface (derived from IDrivable)
public interface ISteerable : IDrivable
{
new void TurnLeft(); // hides TurnLeft() in IDrivable
}
// Car class implements the IMovable interface
class Car : ISteerable
{
// explicitly implement the TurnLeft() method of the ISteerable interface
void ISteerable.TurnLeft()
{
Console.WriteLine("ISteerable implementation of TurnLeft()");
}
// implement the TurnLeft() method of the IDrivable interface
public void TurnLeft()
{
Console.WriteLine("IDrivable implementation of TurnLeft()");
}
}
public class Example8_8
{
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 ISteerable
ISteerable mySteerable = myCar as ISteerable;
Console.WriteLine("Calling mySteerable.TurnLeft()");
mySteerable.TurnLeft();
// cast myCar to IDrivable
IDrivable myDrivable = myCar as IDrivable;
Console.WriteLine("Calling myDrivable.TurnLeft()");
myDrivable.TurnLeft();
}
}
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();
}
}
illustrates deriving an interface from multiple interfaces
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_6.cs illustrates deriving an
interface from multiple interfaces
*/
using System;
public class Example8_6
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// call myCar.Start()
Console.WriteLine("Calling myCar.Start()");
myCar.Start();
// call myCar.TurnLeft()
Console.WriteLine("Calling myCar.TurnLeft()");
myCar.TurnLeft();
// call myCar.Accelerate()
Console.WriteLine("Calling myCar.Accelerate()");
myCar.Accelerate();
}
}
// define the IDrivable interface
public interface IDrivable
{
// method declarations
void Start();
void Stop();
// property declaration
bool Started
{
get;
}
}
// define the ISteerable interface
public interface ISteerable
{
// method declarations
void TurnLeft();
void TurnRight();
}
// define the IMovable interface (derived from IDrivable and ISteerable)
public interface IMovable : IDrivable, ISteerable
{
// method declarations
void Accelerate();
void Brake();
}
// Car class implements the IMovable interface
public class Car : IMovable
{
// declare the underlying field used by the
// Started property of the IDrivable interface
private bool started = false;
// implement the Start() method of the IDrivable interface
public void Start()
{
Console.WriteLine("car started");
started = true;
}
// implement the Stop() methodof the IDrivable interface
public void Stop()
{
Console.WriteLine("car stopped");
started = false;
}
// implement the Started property of the IDrivable interface
public bool Started
{
get
{
return started;
}
}
// implement the TurnLeft() method of the ISteerable interface
public void TurnLeft()
{
Console.WriteLine("car turning left");
}
// implement the TurnRight() method of the ISteerable interface
public void TurnRight()
{
Console.WriteLine("car turning right");
}
// implement the Accelerate() method of the IMovable interface
public void Accelerate()
{
Console.WriteLine("car accelerating");
}
// implement the Brake() method of the IMovable interface
public void Brake()
{
Console.WriteLine("car braking");
}
}
illustrates deriving an interface from one interface
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_5.cs illustrates deriving an
interface from one interface
*/
using System;
// define the IDrivable interface
interface IDrivable
{
// method declarations
void Start();
void Stop();
// property declaration
bool Started
{
get;
}
}
// define the IMovable interface (derived from IDrivable)
interface IMovable : IDrivable
{
// method declarations
void Accelerate();
void Brake();
}
// Car class implements the IMovable interface
class Car : IMovable
{
// declare the underlying field used by the
// Started property of the IDrivable interface
private bool started = false;
// implement the Start() method of the IDrivable interface
public void Start()
{
Console.WriteLine("car started");
started = true;
}
// implement the Stop() methodof the IDrivable interface
public void Stop()
{
Console.WriteLine("car stopped");
started = false;
}
// implement the Started property of the IDrivable interface
public bool Started
{
get
{
return started;
}
}
// implement the Accelerate() method of the IMovable interface
public void Accelerate()
{
Console.WriteLine("car accelerating");
}
// implement the Brake() method of the IMovable interface
public void Brake()
{
Console.WriteLine("car braking");
}
}
public class Example8_5
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// call myCar.Start()
Console.WriteLine("Calling myCar.Start()");
myCar.Start();
// call myCar.Accelerate()
Console.WriteLine("Calling myCar.Accelerate()");
myCar.Accelerate();
}
}
illustrates casting an object to an interface
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_4.cs illustrates casting an object
to an interface
*/
using System;
// define the IDrivable interface
interface IDrivable
{
// method declarations
void Start();
void Stop();
// property declaration
bool Started
{
get;
}
}
// Car class implements the IDrivable interface
class Car : IDrivable
{
// declare the underlying field used by the Started property
private bool started = false;
// implement the Start() method
public void Start()
{
Console.WriteLine("car started");
started = true;
}
// implement the Stop() method
public void Stop()
{
Console.WriteLine("car stopped");
started = false;
}
// implement the Started property
public bool Started
{
get
{
return started;
}
}
}
public class Example8_4
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// use the is operator to check that myCar supports the
// IDrivable interface
if (myCar is IDrivable)
{
Console.WriteLine("myCar supports IDrivable");
}
// cast the Car object to IDrivable
IDrivable myDrivable = (IDrivable) myCar;
// call myDrivable.Start()
Console.WriteLine("Calling myDrivable.Start()");
myDrivable.Start();
Console.WriteLine("myDrivable.Started = " +
myDrivable.Started);
// call myDrivable.Stop()
Console.WriteLine("Calling myDrivable.Stop()");
myDrivable.Stop();
Console.WriteLine("myDrivable.Started = " +
myDrivable.Started);
// cast the Car object to IDrivable using the as operator
IDrivable myDrivable2 = myCar as IDrivable;
if (myDrivable2 != null)
{
Console.WriteLine("Calling myDrivable2.Start()");
myDrivable2.Start();
Console.WriteLine("Calling myDrivable2.Stop()");
myDrivable2.Stop();
Console.WriteLine("myDrivable2.Started = " +
myDrivable2.Started);
}
}
}
illustrates inheriting from a class and implementing multiple interfaces
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_3.cs illustrates inheriting from a class and
implementing multiple interfaces
*/
using System;
interface IDrivable
{
// method declarations
void Start();
void Stop();
// property declaration
bool Started
{
get;
}
}
interface ISteerable
{
// method declarations
void TurnLeft();
void TurnRight();
}
class MotorVehicle
{
// declare the field
private string model;
// define a constructor
public MotorVehicle(string model)
{
this.model = model;
}
// declare a property
public string Model
{
get
{
return model;
}
set
{
model = value;
}
}
}
// Car class inherits from the MotorVehicle class and
// implements the IDrivable and ISteerable interfaces
class Car : MotorVehicle, IDrivable, ISteerable
{
// declare the underlying field used by the
// Started property of the IDrivable interface
private bool started = false;
// define a constructor
public Car(string model) :
base(model) // calls the base class constructor
{
// do nothing
}
// implement the Start() method of the IDrivable interface
public void Start()
{
Console.WriteLine("car started");
started = true;
}
// implement the Stop() methodof the IDrivable interface
public void Stop()
{
Console.WriteLine("car stopped");
started = false;
}
// implement the Started property of the IDrivable interface
public bool Started
{
get
{
return started;
}
}
// implement the TurnLeft() method of the ISteerable interface
public void TurnLeft()
{
Console.WriteLine("car turning left");
}
// implement the TurnRight() method of the ISteerable interface
public void TurnRight()
{
Console.WriteLine("car turning right");
}
}
public class Example8_3
{
public static void Main()
{
// create a Car object
Car myCar = new Car("MR2");
Console.WriteLine("myCar.Model = " + myCar.Model);
// call myCar.Start()
Console.WriteLine("Calling myCar.Start()");
myCar.Start();
// call myCar.TurnLeft()
Console.WriteLine("Calling myCar.TurnLeft()");
myCar.TurnLeft();
}
}

