Skip to main content
Published on

Interfaces in C#

Share:

Introduction

Interfaces are one of the cornerstones of object-oriented programming in C#. They provide a way to create contracts within your software and to ensure that classes implement certain methods or properties.

What is an Interface?

In simple terms, an interface is like a contract. This contract stipulates that any class that implements the interface must also implement its defined methods and properties. However, unlike classes, interfaces do not contain any implementation logic. They only define what must be done, not how.

The Importance of Interfaces

Flexibility and Extensibility

Using interfaces provides flexibility to your code. Since interfaces contain no implementation, they allow you to define multiple implementations for different situations without changing the initial contract.

Testability

By using interfaces, you can create mocks or stubs for your unit tests, allowing you to test components in isolation without external dependencies.

Reusability

You can use the same interface in different parts of your project or in different projects, ensuring that the classes implementing the interface have certain methods and properties.

Practical Example

In the example provided, we have an ICar interface that declares an Info() method. The Car class implements this interface, ensuring that the Info() method is defined.

using System;

namespace Base {
  class Interfaces {
    public void Run() {
      var car = new Car("Nissan", "Juke", "Front", "Manual", "Diesel", 1461, 175, 110, 25070);
      car.Info();

      /*
        Make: Nissan
        Model: Juke
        Engine location: Front
        Transmission: Manual
        Fuel: Diesel
        Displacement: 1461 cc
        Top speed: 175 km/h
        Max power: 110 hp
        Price: 25070 euros
      */
    }
  }

  class Car : ICar {
    private string make, model, engineLocation, transmission, fuel;
    private int displacement, topSpeed, maxPower;
    private double price;

    public Car(string make, string model, string engineLocation, string transmission, string fuel,
                int displacement, int topSpeed, int maxPower, double price) {
      this.make = make;
      this.model = model;
      this.engineLocation = engineLocation;
      this.transmission = transmission;
      this.fuel = fuel;
      this.displacement = displacement;
      this.topSpeed = topSpeed;
      this.maxPower = maxPower;
      this.price = price;
    }

    public void Info() {
      Console.WriteLine($"Make: {make}");
      Console.WriteLine($"Model: {model}");
      Console.WriteLine($"Engine location: {engineLocation}");
      Console.WriteLine($"Transmission: {transmission}");
      Console.WriteLine($"Fuel: {fuel}");
      Console.WriteLine($"Displacement: {displacement} cc");
      Console.WriteLine($"Top speed: {topSpeed} km/h");
      Console.WriteLine($"Max power: {maxPower} hp");
      Console.WriteLine($"Price: {price} euros");
    }
  }

  interface ICar {
    void Info();
  }
}

Considerations When Using Interfaces

When working with interfaces, it is crucial to remember a few points:

  1. Naming: It is a common convention in C# to prefix the name of an interface with the letter 'I', such as ICar.
  2. Simplicity: Keep your interfaces simple and focused. If an interface starts to have too many members, it may be time to refactor or split it.

Conclusion

Interfaces play a crucial role in object-oriented programming, especially in languages like C#. They offer a robust structure that promotes separation of concerns, code reuse, and extensibility. With a firm understanding of interfaces, you will be equipped to create more modular, testable, and maintainable software.

Happy coding!