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.
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.
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;
}
}
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#.