Join Nostr
2026-10-28 10:38:27 UTC

npub1u6…v0yg2 on Nostr: Learn: Interface An interface is a contract: members a class must provide. It can't ...

Learn: Interface

An interface is a contract: members a class must provide. It can't hold instance data (no fields); since C# 8 it may give a method a default body.
A class can implement many interfaces.
Use it when: different classes should be usable the same way, for DI and testing with fakes, or plug-ins.

```csharp
IShape[] shapes = { new Square(2), new Circle(1) };
foreach (var s in shapes) Console.WriteLine(s.Area().ToString("0.00"));

interface IShape { double Area(); }
record Square(double Side) : IShape { public double Area() => Side * Side; }
record Circle(double R) : IShape { public double Area() => Math.PI * R * R; }
```

It prints 4.00, then 3.14.

Read more: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface

#tip · TIP-048