What is an interface?


An interface is a contract that can contain methods, properties, events, indexers, or any combination of those four member types.

What is an interface?

Written by: Dominic Wegrzynowski Oct 04, 2023

´╗┐An interface is a contract that contains methods, properties, events, indexers, or any combination of those four member types. The contract specifies that any class implementing the interface must include implementations of those specified members. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public and they cannot include access modifiers. Members also cannot be static. There are no method or property accessor bodies in an interface. C# classes may implement multiple interfaces and must implement all methods and properties declared in those interfaces.

An interface in C# is a type definition similar to a class, except that it purely represents a contract between an object and its user. It can neither be directly instantiated as an object nor can member data be defined. An interface is a collection of methods and property declarations.

An interface actually provides a common way to interact with all classes that implement it. The ability to work with different classes through the same interface is a form of polymorphism. For example, I have interfaces that declare methods for the service implementations in both my Blog and Bugtracker projects.



Tags

Login to add a comment
0 COMMENT(S)