C# Interfaces Explained

Michael Samteladze
4 min readFeb 28, 2022

--

In this topic I’ll explain what C# interfaces are and how do they work. I’ll try to simplify my explanations as much as possible, so this become a simple read for you.

Fully implemented project, with all code samples is available for download from the link below:
https://github.com/MichaelSamteladze/CSharpInterfacesExplained

What is an Interface?

In a nutshell, Interface is a collection of properties and method names with their return types, without any implementation.

It’s better to name your interfaces with starting capital “I”, where “I” stands for “Interface”. Microsoft names their interfaces like this (IEnumerable, IQueryable, IComparable …)
Remember, It’s not an obligation, you can name your interface as you want, but I’d recommend to follow this pattern in order to keep your code more human readable.

Why do we need an Interface?

What is wrong with classes? why do we need to define methods without implementation? why are we “complicating” everything here?

Let’s say we have a class WeatherReport. It has a method GetCityCurrentTemperature that accepts City as a parameter (name of the wonderful city we live), it makes an API call, to the weather forecast web service and returns nicely formatted string of an air temperature, which we are going to present to our customer.

We are all set, system is up and running, application is presenting air temperature to our customers.

A month later, the web service, we were using for getting air temperature, goes down. What are we going to do? We have to revisit our WeatherReport class, replace all lines of code for GetCityCurrentTemperature method with new WEB API call logic and make sure that TemperatureValue variable is still assigned with the correct value, which is used to format a message for our customer.
If our updated WEB API will also go down, we have to revisit and update GetCityCurrentTemperature method, over and over again.

Is there any way to avoid this? absolutely !!! This is one of many scenarios where interfaces come in rescue.

Instead of writing an implementation for WEB API inside WeatherReport class, let’s provide this class with some sort of object that will take care for making API calls. Considering our bad experience of code rewriting from above, we don’t want to be bound for a specific implementation any more. We need something that has no details and guarantees the result! we need an interface !!!

Let’s modify our code sample from above for a little bit:

  1. Line 3: We’ve defined a private field WeatherServiceProvider, which is type of IWeatherService interface (that we’ve created in very beginning).
  2. Line 5: We’ve defined a constructor for the class, that receives WeatherServiceProvider object
  3. Line 14: We call GetCityTemperatureCelsius method from WeatherServiceProvider to get a result value we are looking for.

As you can see an updated code has no implementation of any API calls, we are just using a method definition GetCityTemperatureCelsius from IWeatherService interface and it guarantees us the result which will be a type of decimal? (nullable decimal).

This technique is call ABSTRACTION.

In other words, when a functional logic of your class is bound to a definition and not a specific implementation it’s called an ABSTRACTION.

Interface Implementation

WeatherReport class functionality is done, no matter which WEB API service is up or down, WeatherReport class needs no changes! period! Abstraction worked just perfect, but we still need an implementation, somebody who will actually make an API call. So let’s implement our IWeatherService interface:

We’ve just created two classes AwesomeWeatherService and WonderfulWeatherService, both are inherited from IWeatherService interface (that’s how we implement interfaces).

Don’t be mistaken, this is not a traditional C# inheritance with derived and base classes. This is inheritance where IWeatherService tells AwesomeWeatherService and WonderfulWeatherService what is their minimum obligation to exists.
In our particular scenario, both classes are obligated to have a method named GetCityTemperatureCelsius that returns decimal? result.
Interface obligates classes to implement properties and methods it has. That’s why classes, in C#, are allowed to be inherited from multiple interfaces.

Wrapping Up

We have a class WeatherReport class, we have interface implementations, time to display an air temperature of our wonderful city.

Line 5: we defined WeatherService variable who can make an API call

Line 8: we passed this variable to the constructor of WeatherReport class. We are allowed to do this, because WeatherReport constructor accepts parameter of type IWeatherService and AwesomeWeatherService is inherited from
IWeatherService.

Line 9: we receive nicely formatted string.

Line 11: we display nicely formatted string to our customers.

If AwesomeWeatherService goes down, all we need to do is simply replace AwesomeWeatherService with WonderfulWeatherService at Line 5.

No other changes are required because WonderfulWeatherService is also inherited from IWeatherService.

Any instance of class, that is inherited from IWeatherService interface, can be assigned WeatherServiceProvider variable and no other changes are needed to make our code work.

Fully implemented project, with all code samples is available for download from the link below:
https://github.com/MichaelSamteladze/CSharpInterfacesExplained

--

--