# Software Interfaces

A software interface is a contract between pieces of code. It lists what a class can do, never how, so code can use any class that keeps the contract. .NET is full of them, such as IEnumerable&lt;T>.

Apprentice · C# · OOP · Updated November 3, 2026

## After this lesson you can

- Explain an interface as a contract that lists what a class can do, without saying how

## Key takeaways

- An interface is a contract that lists what a class can do, never how it does it.
- Code that asks for an interface works with any class that keeps it, so `PrintAll(IEnumerable<string> names)` takes a list, an array or a set.
- Interface names in .NET start with a capital I, as in `IEnumerable<T>`.
- A base class gives its derived classes real code; an interface gives them only a list of what they must be able to do.

A thermostat, a phone app and a voice assistant are three **user** interfaces to the same heating system. Each lets a person interact with the software in its own way.

Code needs to interact with other code too, and a screen or a voice won't do. Instead, it uses a **software interface**: a list of what one piece of code promises another. Think of it as a **contract**:

- The class that **keeps** the contract promises: *"I can do every one of these things."*
- The code that **uses** it relies on exactly that list, and nothing else.

Like a wall socket. Any lamp, phone charger or toaster with the right plug works in it. The socket doesn't care what's on the other end of the cord, only that the plug fits.

## You've already used one

.NET is full of interfaces, and their names start with a capital **I**. One you've relied on since your first loop is **`IEnumerable<T>`**: the contract for *"you can go through my items one by one."* `foreach` works on anything that keeps it.

So a method that asks for an `IEnumerable<string>` accepts a list, an array or a set, because all three keep that contract:

```csharp
List<string> list = new List<string> { "Ada", "Grace" };
string[] array = { "Linus", "Guido" };
HashSet<string> set = new HashSet<string> { "Anders" };

PrintAll(list);
PrintAll(array);
PrintAll(set);

void PrintAll(IEnumerable<string> names)
{
    foreach (string name in names)
    {
        Console.Write(name + " ");
    }
    Console.WriteLine();
}
```

```text
Ada Grace
Linus Guido
Anders
```

`PrintAll` doesn't know or care whether it was given a list, an array or a set. It only knows the contract: it can go through the items one by one. That's the same idea as the socket.

## What and how

A contract says **what**, never **how**. `IEnumerable<T>` promises that you can go through the items; a `List<T>` does it by stepping through an array inside it, a `HashSet<T>` does it its own way. The code that uses the contract never has to know.

That's the difference from a base class. A base class hands its derived classes properties and finished methods: real code they inherit. An interface hands them **nothing but a list** of what they must be able to do. The next lessons show why that's useful, and how to write your own.

## Check yourself

1 question to try on the lesson page.

## So what

An interface lets code say *"I'll work with anything that can do this,"* and mean it. Any class that keeps the contract fits, today's and tomorrow's.

## Key terms

- [Interface](https://www.ask-codey.com/glossary/interface-c-sharp)

## Cheat sheets

- [Software Interfaces](https://www.ask-codey.com/cheat-sheets/interfaces-in-csharp-software-interfaces-cheat-sheet)

Source: https://www.ask-codey.com/lessons/interfaces-in-csharp-software-interfaces
