Skip to content

Tony Cimaglia

🌱 Understanding Interfaces In C#

c#, dotnet1 min read

🌱 This post is a part of my digital garden. It's a place where ideas grow. It's not a polished article, but more of a living breathing breathing document. 🌱


We're not talking about user interfaces, so what is an interface anyway?

Do you remember the Nintendo64? Different cartriges (games) could be popped into and out of any console. That's because there was a specification somewhere that layed out exactly how the two pieces connect together. Every single N64 cartrige and console in the world was designed and built according to those same specifications.

In software, interfaces describe how a class is used by another class.

benefits behavior from multiple source (can't inherit multiple classes in C#) must use an interface for a struct as well (can't inherit from classes / other structs) I convention

1interface IBasicCalculator {
2 int Add(int firstNumber, int secondNumber);
3 int Subtract(int firstNumber, int secondNumber);

the interface doesn't care how the numbers are added, just that there is an add / subtract method.

Let's try to think about implementing an interface in a (fake) real life scenario. Say we have a software product called HappyCustomer that is used by other businesses. One portion of the application helps them handle customer relationship management (CRM). It only does a few things:

  • It searches for customers by name.
  • It finds the customer by their id.
  • It has a method to update the customer's status.

Here's the customer class that represents each record in the table :

1public class Customer
3 public int CustomerId;
4 public string FirstName;
5 public string LastName;
6 public string CustomerStatus;

And here's the CRM class:

1using System.Collections.Generic;
3public class HappyCustomerCRM
4 {
5 public List<Customer> Search(string searchText)
6 {
7 List<Customer> SearchResults = new List<Customer>();
8 SQLService Service = new SQLService();
9 SearchResults = Service.SearchCustomers(searchText);
10 return SearchResults;
11 }
13 public Customer GetById(int customerId)
14 {
15 Customer Customer = new Customer();
16 SQLService Service = new SQLService();
17 Customer = Service.GetCustomerById(customerId);
18 return Customer;
19 }
21 public Customer UpdateStatus(int customerId, string newStatus)
22 {
23 Customer UpdatedCustomer = new Customer();
24 SQLService Service = new SQLService();
25 Customer = Service.UpdateCustomerStatus(customerId, newStatus);
26 return Customer;
27 }
28 }
  • The Search method takes the search text that user inputs, finds the records that match the text in SQL, and returns a List of Customers that match the text.
  • The GetById method takes a CustomerId, finds the record in SQL, and returns the customer. Easy.
  • The UpdateStatus method takes in the customerId and the newStatus, updates the record in the table, and returns the updated Customer.

The compnay has never had a reason to change this code, but there's a problem. They're courting a new client who already uses Salesforce to handle customer relationship management. They want to use Deco's software, but they will only do so if they can use their existing CRM.

They could create a brand new class with it's own methods

© 2021 by Tony Cimaglia. All rights reserved.