C# Reference ยท Keywords and Statements
The interface keyword
Reference for the C# interface keyword: declare contracts, implement them implicitly or explicitly, use default and static abstract members.
An interface defines a contract: a set of members that a class or struct promises to provide. It says what a type can do without saying how. A type can implement many interfaces, which is how C# gets polymorphism without multiple inheritance of classes.
Interface members are public by default and can be methods, properties, indexers and events. Since C# 8 they can also have default implementations, and since C# 11 they can declare static abstract members, which makes generic math and factory patterns possible. By convention interface names start with I.
interface IShape { double Area(); string Name => "shape"; // default implementation }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[modifiers] interface IName<T> [: IBase1, IBase2] [where T : constraint] { ReturnType Method(params); // abstract ReturnType Method2(params) => expr; // default implementation (C# 8) Type Property { get; set; } event EventHandler Changed; Type this[int i] { get; } static abstract Type Create(); // C# 11 static Type Helper() => ...; // static member with body }
Members may carry access modifiers (private, protected, internal) when they have a body. Variance annotations in and out apply to interface type parameters (IEnumerable<out T>).
Errors: a class that does not implement every abstract member does not compile (CS0535). Implementing a member with the wrong accessibility (not public) is CS0737.
Usage notes
- Implicit versus explicit: implement a member as
publicfor normal use, or asvoid IFoo.Bar()to hide it from the class and to resolve name clashes between interfaces. - Default members are called only through the interface type (
((IShape)x).Name). They help evolve published interfaces without breaking implementers, but do not use them to replace base classes. - Diamond problem: if two interfaces a class inherits provide different default implementations of the same member, the class must supply its own.
- Structs that implement an interface are boxed when treated as that interface. A generic constraint (
where T : IFoo) avoids boxing. - Interface versus abstract class: use an interface for capabilities across unrelated types; use an abstract class when you need shared state or constructors.
- No fields, no instance constructors. Interfaces cannot hold instance state.
- Marker interfaces (no members) are legal, but attributes are usually a better tool.
Related
kw-class,kw-abstract,kw-structkw-staticandkw-virtual-override- Tutorials: Polymorphism, Abstract classes, Variance, Generic math