What is an interface in C#?
Answer
An interface in C# defines a contract — a set of method, property, event, and indexer signatures that implementing types must provide. Interfaces cannot contain instance fields (but can have static fields and default implementations in C# 8+). Syntax: public interface IShape { double Area(); double Perimeter(); }. A class or struct implements interfaces with: class Circle : IShape { public double Area() => Math.PI * r * r; ... }. Key points: (1) C# supports multiple interface implementation: class Foo : IBar, IBaz. (2) Interfaces enable polymorphism and dependency injection. (3) Interface members are public by default. (4) C# 8+ allows default interface implementations (DIM) — base behavior without breaking existing implementors. (5) Prefer interfaces over abstract classes for defining contracts when no shared state is needed. Commonly named with I prefix (IEnumerable, IDisposable).
Previous
What is polymorphism in C#?
Next
What is the difference between abstract class and interface in C#?