Ana Sayfa Hakkımda Dersler Blog MikrofondaOn The Mic İletişim Projeler Yakında
DerslerLessons TEMEL · C#

C# Temelleri — Interface C# Basics — Interface

9 dk okuma 9 min read · Emre Ulutabak
1
Interface nedir? What is an interface?

Bir kafede garson çalışıyor. Müşteri garsona şunu söyler: "Sipariş al, servis yap, hesabı getir." Müşteri garsonun nasıl yapacağıyla ilgilenmez — sadece ne yapacağını bilir.

İşte interface tam olarak budur. Bir sınıfın ne yapacağını tanımlayan ama nasıl yapacağına karışmayan bir sözleşmedir.

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
Neden kullanılır? Why use it?

Diyelim ki bir ödeme sistemi yazıyorsun. Kredi kartıyla da ödeme yapılabilir, havaleyle de. İkisi de farklı çalışır ama ikisi de ödeme yapar.

Interface sayesinde kodun geri kalanı nasıl ödeme yapıldığını bilmek zorunda kalmaz. Sadece IOdemeServisi'ni kullanır.

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
Nasıl yazılır? 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 isimleri geleneksel olarak büyük 'I' harfiyle başlar: IEmailService, IRepository gibi. Interface names traditionally start with a capital 'I': IEmailService, IRepository, and so on.
4
Sınıfla farkı Difference from a class

Sınıf hem ne yapacağını hem nasıl yapacağını içerir. Interface yalnızca ne yapacağını tanımlar — içinde kod olmaz, sadece imza vardır.

Bir sınıf birden fazla interface'i uygulayabilir. Bu, C#'ta çoklu kalıtımın kapısıdır.

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
Altın kurallar Golden rules
💡
Interface bir sözleşmedir. Onu uygulayan sınıf tüm metotları yazmak zorundadır. An interface is a contract. Any class that implements it must write all the methods.
💡
Interface'ler dependency injection'ın temelidir. ASP.NET Core'da her servis bir interface üzerinden enjekte edilir. Interfaces are the foundation of dependency injection. In ASP.NET Core, every service is injected through an interface.
💡
Somut sınıfa değil, interface'e bağımlı ol. Bu SOLID'in 'D' si: Dependency Inversion. Depend on the interface, not the concrete class. This is the 'D' in SOLID: Dependency Inversion.
MİNİ QUIZ MINI QUIZ
Interface hakkında hangisi doğrudur? Which statement about interfaces is correct?
Interface içinde metot gövdesi yazılabilir You can write method bodies inside an interface
Bir sınıf yalnızca bir interface uygulayabilir A class can only implement one interface
Interface ne yapılacağını tanımlar, nasıl yapılacağını değil An interface defines what will be done, not how
Interface isimleri küçük harfle başlar Interface names start with a lowercase letter