Mega Code Archive

 
Categories / C# / Class Interface
 

Illustrates interfaces

/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /*   Example8_1.cs illustrates interfaces */ using System; // define the IDrivable interface public 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_1 {   public static void Main()   {     // create a Car object     Car myCar = new Car();     // call myCar.Start()     myCar.Start();     Console.WriteLine("myCar.Started = " + myCar.Started);     // call myCar.Stop()     myCar.Stop();     Console.WriteLine("myCar.Started = " + myCar.Started);   } }