Title: Define and implement an interface in C#
(This example doesn't do anything. It just shows how to define an interface.)
An interface defines properties, methods, and events for a class but doesn't provide an implementation for them. A class that implements the interface must provide code for the defined properties, methods, and events.
This lets a program treat objects that implement an interface in the same way. For example, the IComparable interface requires a CompareTo method that compares two objects. If you write a method that takes an array of IComparable objects as a parameter, then the code can use the objects' CompareTo methods to compare them without knowing what class the objects actually are. The method can compare objects of any class as long as it implements IComparable.
This example uses the following code to define an IVehicle interface.
public interface IVehicle
{
int MaxSpeed { get; set; }
float Mpg { get; set; }
}
The code declares the interface and gives it two properties: MaxSpeed and Mpg. Notice that it does not provide an implementation for the properties.
The following code defines a Car class that implements IVehicle.
public class Car : IVehicle
{
// Implement IVehicle.MaxSpeed explicitly.
private int _MaxSpeed;
int IVehicle.MaxSpeed
{
get { return _MaxSpeed; }
set { _MaxSpeed = value; }
}
// Implement IVehicle.Mpg implicitly.
private foat _Mpg;
public float Mpg
{
get { return _Mpg; }
set { _Mpg = value; }
}
// Add a new property.
private int _NumCupholders;
public int NumCupholders
{
get { return _NumCupholders; }
set { _NumCupholders = value; }
}
}
The : IVehicle part of the class declaration indicates that the class implements the IVehicle interface. Because it implements the interface, the class must provide an implementation for the MaxSpeed and Mpg properties.
This class uses a backing field to implement its properties. It stores values in a private variable and the get and set accessors simply return and set the private value.
There are also two ways a class can implement part of an interface: explicitly or implicitly.
To implement a property explicitly, prefix the property's name with the name of the class as in IVehicle.MaxSpeed. When you do this, the program can only access the property by using the interface and not by an object from the class. In this example, the main program cannot access a Car object's MaxSpeed property. Instead it must make an IVehicle variable that points to a Car object and then it can use the variable's MaxSpeed property.
To implement a property implicitly, simply omit the prefix as in the Mpg property shown in the code above. When you do this, the program can access the property for either a class variable (Car) or an interface variable (IVehicle).
After it implements the MaxSpeed and Mpg properties, the Car class adds a new NumCupholders property. This isn't defined by the interface so interface variables don't know about it and can't use it.
The example program's Form_Load event handler uses the following code to demonstrate the interface.
// Make some objects.
private void Form1_Load(object sender, EventArgs e)
{
// Make a Vehicle.
// Set NumCupholders and Mpg but not MaxSpeed.
Car car = new Car();
car.NumCupholders = 5;
car.MaxSpeed = 50; // Fails.
car.Mpg = 10;
// Make an IVehicle.
// Set MaxSpeed and Mpg but not NumCupholders.
IVehicle ivehicle = new Car();
ivehicle.NumCupholders = 5; // Fails.
ivehicle.MaxSpeed = 50;
ivehicle.Mpg = 10;
}
First the program creates a Car object and tries to set its NumCupholders, MaxSpeed, and Mpg properties. Because MaxSpeed is implemented explicitly, the code can't set its value through the Car variable.
Next the code creates another Car object but saves it in a variable of type IVehicle. The Car class implements IVehicle so any Car is also an IVehicle. Using this variable, the code can access the MaxSpeed and Mpg properties defined by the interface. It cannot access the NumCupholders property, however, because IVehicle doesn't define that property.
Download the example to experiment with it and to see additional details.
|