How to use Interfaces in C#

Interfaces are a way of building classes with basic inheritance in .Net. An interface can't have properties or any form of code in them. If you have the below interface, it will not compile because it has instantiable code in it, remove the string variable and it will compile so long as you correct the other mistake but more about that later.

public interface Vehicle
{
    string sRegistration; // This line will not compile.
    void SetRegistration(string sReg);
    string GetRegistration();
    bool HasStopped();
    void ChangeSpeed(int iChange);
    int NumberOfWheels();
}

Now we create two types of Vehicle objects where both can have code and properties. All the interface functions must be inherited and coded otherwise it will not compile. We're doing a very basic model here to show what can be done. Note that a car has two extra functions which the Motorbike class doesn't.

public class Car : Vehicle
{
    int iSpeed = 0;
    string sRegistration = "";
    public string GetRegistration() { return sRegistration; }
    public string GetColour() { return "Blue": }
    public bool HasStopped() { return iSpeed == 0; }
    public void SetRegistration(string sReg) { sRegistration = sReg; }
    public void ChangeSpeed(int iChange) { iSpeed += iChange; }
    public void IncreaseSpeed() { iSpeed++; }// Two unique functions for this class.
    public void DecreaseSpeed() { if (iSpeed > 0) iSpeed--; }
    public int NumberOfWheels() { return 4; }
}

public class MotorBike : Vehicle
{
    int iSpeed = 0;
    string sRegistration = "";
    public void SetRegistration(string sReg) { sRegistration = sReg; }
    public string GetRegistration() { return sRegistration; }
    public bool HasStopped() { return iSpeed == 0; }
    public void ChangeSpeed(int iChange) { iSpeed += iChange; }
    public int NumberOfWheels() { return 2; }
}

Now that we've created our interfacable (Might not be in the dictionary but you can guess what I mean.) classes we can now use them. We'll create two objects, a Car and a Bike. You create an object of each type normally. If both objects have the same interface immaterial of whether they have different additional functions, you can add them to a List collection as you see below.

class Program
{
    static void Main(string[] args)
    {
        Car oCar = new Car();
        oCar.SetRegistration("Beta");
        Console.WriteLine("Number of Wheels : " + oCar.NumberOfWheels());
        oCar.IncreaseSpeed();

        MotorBike oBike = new MotorBike();
        oBike.SetRegistration("Alpha");
        oBike.IncreaseSpeed(); // Will not compile as Bike doesn't have IncreaseSpeed, you'd need to use the ChangeSpeed funcion.         Console.WriteLine("Number of Wheels : " + oBike.NumberOfWheels());

        List<Vehicle> oVehicles = new List();
        oVehicles.Add(oCar);
        oVehicles.Add(oBike);

        // This will not compile, you can't add a Bike to a Car list.
        List<Car> oCarList = new List();
        oCarList.Add(oBike);

        foreach(Vehicle oVeh in oVehicles)
        {
            if(oVeh.GetType() == typeof(Car))
            {
                Console.WriteLine("Registration : " + oVeh.GetRegistration());
                Console.WriteLine("Colour : " + ((Car)oVeh).GetColour());
            }
        }

    Console.ReadKey();
    }
}

The only references you will need for this are below which are normally automatically added to the solution so you won't need to worry about which ones you've missed.

using System;
using System.Collections.Generic;

To summarise Interfaces -

public interface Boat {
    public void Sail();
}

public class AmphibiousCraft : Vehicle , Boat
{
}

Tags - .NET

Last Modified : June 2023


About... / Contact.. / Cookie...