Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — Interface

9 min read · Emre Ulutabak
1
What is an interface?

A waiter works at a café. The customer tells the waiter: "Take orders, serve food, bring the bill." The customer doesn't care how the waiter does it — they only know what will be done.

That is exactly what an interface is. It is a contract that defines what a class will do, without caring how it does it.

2
Why use it?

Say you're building a payment system. You can pay by credit card or by bank transfer. Both work differently but both make a payment.

Thanks to interfaces, the rest of the code doesn't need to know how payment is made. It just uses IPaymentService.

3
How to write one?
csharp
public interface IOdemeServisi
{
    void OdemeYap(decimal tutar);
    bool OdemeDogrula(string referansNo);
}

public class KrediKartiServisi : IOdemeServisi
{
    public void OdemeYap(decimal tutar)
    {
        Console.WriteLine($"Kredi kartıyla {tutar} TL ödeme yapıldı.");
    }

    public bool OdemeDogrula(string referansNo)
    {
        // doğrulama mantığı...
        return true;
    }
}
💡
Interface names traditionally start with a capital 'I': IEmailService, IRepository, and so on.
4
Difference from a class

A class contains both what it does and how it does it. An interface only defines what will be done — no code inside, only method signatures.

A class can implement multiple interfaces. This is the door to multiple inheritance in C#.

5
Golden rules
💡
An interface is a contract. Any class that implements it must write all the methods.
💡
Interfaces are the foundation of dependency injection. In ASP.NET Core, every service is injected through an interface.
💡
Depend on the interface, not the concrete class. This is the 'D' in SOLID: Dependency Inversion.
MINI QUIZ
Which statement about interfaces is correct?
You can write method bodies inside an interface
A class can only implement one interface
An interface defines what will be done, not how
Interface names start with a lowercase letter