Home > CRM > C# Basics | Why Interfaces?

C# Basics | Why Interfaces?

I often get this question “Why Interfaces”, can’t I simply implement logic just with Class from C# beginners?.

Well, If you search this questions over internet, you get the following overwhelming response:

In C#, interfaces are a way to specify a contract that a class must implement. They allow you to define a set of methods that a class must implement, without specifying how those methods should be implemented. This is useful for several reasons:

  1. Interfaces allow you to create a common set of methods that can be used across multiple classes, even if those classes have very different implementations.
  2. Interfaces allow you to write code that is more flexible and adaptable, because it is not tied to a specific implementation of a class.
  3. Interfaces can be used to model real-world concepts in your code, such as the concept of a “drawable” object that can be drawn to the screen.
  4. Interfaces can be used to create a level of abstraction between different parts of your code, which can make it easier to understand and maintain.

Overall, interfaces are a powerful feature of C# that can help you write more flexible, reusable, and maintainable code.

Why Interfaces in C#?

In this article, I will try to explain a very basic use of Interface out of many other benefits.

Scenario:

Assume that you are building a Shopping Cart functionality for a Car Show room, which ships both Electric and Gas Cars and you want build functionality to add cars to the Cart and Ship them.

That’s the scenario and I will demonstrate this scenario with a simple C# console project.

Implementation:

We will first start with defining ‘Entity Model’ and then move on to Cart functionality.

Entity Model :
  • As we know that ‘Electric Car’ and ‘Gas Car’ contain both common and different features, So I decided to go with two classes ElectricCar and GasCar.
  • Class ‘ElectricCar‘ looks as below:
  • Class ‘GasCar‘ looks as below:
  • If you notice, “FullChargeTimeInHours” property is in ‘ElectricCar‘ but not in ‘GasCar‘ and “CapacityInLiters” property is in ‘GasCar’ but not in ‘ElectricCar‘.
  • Lets implement a simple ‘Cart’ functionality by:
    • Add items to Cart
    • And ship them using ‘Ship()’ function

Cart Logic (No Interface):
  • Add a new function AddElectricCars() which returns list of Electric Cars.
  • Add a new function AddGasCars() which returns list of Gas Cars.
  • Now call AddElectricCars() and AddGasCars() and loop through Cars and invoke Ship() function.
  • If you observe, both ‘ElectricCar‘ and ‘GasCar‘ classes have same properties and function (i.e., Ship) except one property, we had to implement 2 Carts logic.
  • Now lets see how this can be simplified using Interfaces.

Cart logic (using Interface):
  • Create a new ‘ICar’ interface with common properties and function between ‘ElectricCar‘ and ‘GasCar‘.
  • Implement ‘ICar’ interface in ‘ElectricCar‘ class.
    • Notice that I’ve defined ‘FullChargeTimeInHours’ property which is specific to ‘ElectricCar‘ with in the Class.
  • Similarly implement ‘ICar’ interface in ‘GasCar‘ class.
    • Notice that I’ve defined ‘CapacityInLiters’ property which is specific to ‘GasCar‘ with in the Class.
  • Now lets see the magic. We can have a single Cart which handles both ‘ElectricCar‘ and ‘GasCar‘.
  • Add just one ‘AddCars()’ function which returns list of ‘ICar’.
  • If you execute the code, the output looks as below:
  • That’s the flexibility we get with interfaces. I hope now you can relate the statement “Interfaces allow you to create a common set of methods that can be used across multiple classes, even if those classes have very different implementations.” with our Cars example.

You can download the whole code from here. I will continue this C# basics with more simple examples.

🙂

Categories: CRM
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: