Building Robust Systems with C# Interfaces

Facundo Revello
April 24, 2020

C# is a general-purpose, object-oriented programming language developed around 2000 by Microsoft within its .NET. In this post, we’ll explain the benefits of using C# interfaces to build robust systems and show how to implement them.

Introduction to C# Interfaces

Imagine you are the head of an organization. In order to operate properly, you can't do everything in-house, so you will need to acquire some third party services that will help you operate according to expectations. What you can do to solve these problems is contracting these services so they can provide the help you need. Imagine now that you want to change one of the services you are contracting for another because it offers you more benefits.

C# interfaces does exactly that. It offers developers a way of making these “contracts” with services that our system uses. If for some reason we want to change to a new one, this can be done easily without compromising the rest of the system. The main idea behind these interfaces is to modularize the system, separating the different sections of our system such as UI, business logic, database handling, etc. It allows the different sections to avoid dependencies between each other and makes it easy to maintain and update the different modules.

How C# Interfaces Work

From a programming point of view, C# interfaces are similar to a class in some aspects. Unlike class interfaces, C# interfaces do not have access modifiers such as public or protected, and the methods do not have an implementation or body, so by default all members of an interface are abstract and public. It's important to note that as a convention, the interface name must start with an I. So how do these interfaces work?

The best way of explaining how the interfaces work is with an example. Imagine we have a car rental business. Each car has a price that is calculated from several attributes, including the age of the renter and the region where the car can be driven. We may have something like this: a CarRental, a ICarInformation and a CarInformation implementation of the interface:

Now imagine that according to a new business logic, the way of calculating the price of renting a car changed. Or maybe there is a new law that changes the regions in which renters can drive the car. This can be easily changed from the implementation of the interface side. The new price calculator can be changed, and the way the region is calculated can be changed. Or, in the worst scenario in which a new complete CarInformation implementation is necessary, it can easily replace the old one. Thanks to the interfaces, this will not have any impact on the rest of the system, and no changes must be done on other sections of the system to allow this new change. As the CarRental will still be using the methods from the interface, it will not know that the implementation of the interface has changed.

The Benefits of Using C# Interfaces

C# interfaces are extremely powerful. They provide the possibility of modularizing your system, making it possible to improve the extensibility. This allows you to avoid dependencies between the different components of your system, reducing the impact of changing these services to a minimum. Another important aspect is that these interfaces help us maintain the system.

C# is a powerful language that provides developers with many useful tools and resources such as interfaces, which are a great option for developing robust systems.

"Building Robust Systems with C# Interfaces" by Facundo Revello is licensed under CC BY SA. Source code examples are licensed under MIT.

Photo by sophilabs.

Categorized under research & learning.

Join our team

If you're passionate about building quality software and our values resonate with you, get in touch with us!