BLOG.CSHARPHELPER.COM: Define and implement an interface in C#
Define and implement an interface in C#
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 implementations for the defined properties, methods, and events.
This lets a program treat objects that implement the 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 without knowing what class the objects actually are. The method can compare objects of any class as long as it implements IComparable.
The following code defines an IVehicle interface in C#.
public interface IVehicle { // Define a MaxSpeed property. int MaxSpeed { get; set; } }
The code declares the interface and gives it a single property named MaxSpeed. Notice that it does not provide an implementation for the property.
The following code defines a Car class that implements IVehicle.
public class Car : IVehicle { // Implement IVehicle.MaxSpeed private int _MaxSpeed; int IVehicle.MaxSpeed { get { return _MaxSpeed; } set { _MaxSpeed = value; } }
// Add a new property. private int _NumCupholders; public int NumCupholders { get { return _NumCupholders; } set { _NumCupholders = value; } } }
The declaration for the Car class indicates that the class implements the IVehicle interface. Because it implements the interface, it must provide an implementation for the MaxSpeed property. Notice how the property implementation includes the prefix "IVehicle."
The class also adds a new NumCupholders property.
When the program starts, it uses the following code to demonstrate the interface.
// Make some objects. private void Form1_Load(object sender, EventArgs e) { // Make a Vehicle. // IntelliSense sees NumCupholders but not MaxSpeed. Car car = new Car(); car.NumCupholders = 5; car.MaxSpeed = 50; // Fails.
// Make an IVehicle. // IntelliSense sees MaxSpeed but not NumCupholders. IVehicle ivehicle = new Car(); ivehicle.NumCupholders = 5; // Fails. ivehicle.MaxSpeed = 50; }
First the program creates a Car object. Even though Car implements the IVehicle interface, the Car object doesn't directly support the MaxSpeed property. Notice the flagged text in the picture. (This seems odd to me. Car clearly has a MaxSpeed property defined for the interface and defining a different MaxSpeed property would be very confusing. Visual Basic handles this much more naturally by letting you access the MaxSpeed property directly.)
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 property defined by the interface. It cannot access the NumCupholders property, however, because IVehicle doesn't define that property. (That makes more sense. If a method can process any IVehicle object, that object might be a Car, Motorcycle, or Skateboard object so you don't know if it has a NumCupholders property.)
The next few posts will cover other useful techniques for using interfaces.
Comments