Thuta Learning
C#
AdvancedProgrammingBeginner

Interfaces

C#Lesson 2125 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Interfaces
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Interfaces, in plain language

Interface is a contract that spells out which methods/properties a class must have. Any class that implements the interface has to provide all the members defined inside it.

Problem → Why → Solution

The problem — why this concept matters

Copying every method of a class into an interface does not create a meaningful abstraction.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

An interface is a capability contract rather than an implementation and supports decoupling and testing across types. Keep interfaces small and consumer-focused, and define behavioral expectations with documentation and tests.

Mental model
  1. object or dependency
  2. C# type contract and CLR
  3. reusable behavior

Explain the core concept and runtime behavior of Interfaces

Core syntax or pattern

csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

An interface is a capability contract rather than an implementation and supports decoupling and testing across types. Keep interfaces small and consumer-focused, and define behavioral expectations with documentation and tests.

Read the code step by step

  1. object or dependencyinterface INotification

    Interface is a contract that spells out which methods/properties a class must have. Any class that implements the interface has to provide all the members defined inside it.

  2. C# type contract and CLRpublic void Send(string message)

    An interface is a capability contract rather than an implementation and supports decoupling and testing across types. Keep interfaces small and consumer-focused, and define behavioral expectations with documentation and tests.

  3. reusable behavior}

    Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

Execution flow
  1. object or dependency
  2. C# type contract and CLR
  3. reusable behavior

Success

Explain the core concept and runtime behavior of Interfaces

Failure

Copying every method of a class into an interface does not create a meaningful abstraction.

Interface is a contract that spells out which methods/properties a class must have. Any class that implements the interface has to provide all the members defined inside it.

csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

Where interfaces come in handy

You can send a notification through email, SMS, push notification, or whatever else — but your app's main logic only needs to know about one Send() method. Want to change the implementation? Just swap out the class.

You should see
Email sent: Welcome to ThutaTech

Real-world usage

How to apply it in a real project

Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

Mini challenge

Build it yourself

Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

Expected: Explain the core concept and runtime behavior of Interfaces

Show hint

Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

Quick quiz 1 / 3

What best describes the core idea of Interfaces?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • object or dependency → C# type contract and CLR → reusable behavior
  • An interface is a capability contract rather than an implementation and supports decoupling and testing across types. Keep interfaces small and consumer-focused, and define behavioral expectations with documentation and tests.
  • Copying every method of a class into an interface does not create a meaningful abstraction.
  • Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

Object-oriented programming in C# — Microsoft Learn

Easy traps

  • Copying every method of a class into an interface does not create a meaningful abstraction.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Define IContactStore with in-memory and fake test implementations, separating UI and business logic from storage.

You'll know it worked when: Email sent: Welcome to ThutaTech

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Interfaces | Thuta Learning