Interfaces in the Common Type System

An interface defines a contract that specifies a "can do" relationship or a "has a" relationship. Interfaces are often used to implement functionality, such as comparing and sorting (the IComparable and IComparable<T> interfaces), testing for equality (the IEquatable<T> interface), or enumerating items in a collection (the IEnumerable and IEnumerable<T> interfaces). Interfaces can have properties, methods, and events, all of which are abstract members; that is, while the interface defines the members and their signatures, it leaves it to the type that implements the interface to define the functionality of each interface member. This means that any class or structure that implements an interface must supply definitions for the abstract members declared in the interface. An interface can require that any implementing class or structure must also implement one or more other interfaces.

The following restrictions apply to interfaces:

  • An interface can be declared with any accessibility, but interface members must all have public accessibility.

  • No security permissions can be attached to members or to the interface itself.

  • Interfaces cannot define constructors.

  • Interfaces cannot define fields.

  • All abstract properties, methods, and events defined in an interface must be instance members; they cannot be static members.

Each language must provide rules for mapping an implementation to the interface that requires the member, as more than one interface can declare a member with the same signature and these members can have separate implementations.

See Also

Concepts

Type Members

Security Permissions

Other Resources

Common Type System