Interface

Why Trust Techopedia

What Does Interface Mean?

Interface, in C#, is a code structure that defines a contract between an object and its user. It contains a collection of semantically similar properties and methods that can be implemented by a class or a struct that adheres to the contract.

Advertisements

In general, an interface is used to describe a set of related functionalities that can be implemented in a class or struct. It enables a class to inherit multiple behaviors defined in multiple interfaces. It also helps to resolve the name ambiguity that arises while using multiple methods with same names existing in different interfaces.

Designing applications using interfaces helps to address the vital requirements of flexible systems such as loose coupling, separation of concerns and adaptability to future changes. Due to the isolation of components that implement the interfaces of an application, testing these components becomes easier. The .NET Framework library uses many generic interfaces (whose types are parameterized) in collection classes to represent items in a collection so that the boxing and unboxing operations on value types are avoided.

Techopedia Explains Interface

An interface is a construct using the keyword “interface” and is similar to an abstract class but without any implementation code. It is implemented using a colon (:) in C#.

For example, IDisposabe is an interface that can be implemented by a C# class to clean up the resources used by the objects of that class type.

The key features of an interface include:

  • It can be declared within a name space or a class.
  • Its members can be a method, property, event or indexer, but cannot be a constant, field, operator, instance constructor, destructor, type or static member.
  • It cannot be instantiated as an object and cannot be defined with data members.
  • Unlike a class that can inherit only one base class and multiple interfaces, an interface can inherit multiple interfaces only.
  • An interface member has public access by default, which cannot be modified using any access modifiers.
  • Modifiers, such as virtual, override or static, should not be used with an interface member.
  • An interface can be accessed using its fully qualified name, which includes the interface name followed by a dot and the member name.
  • An interface member with the “new” modifier can be used to hide an inherited member with the same name.
Advertisements

Related Terms

Margaret Rouse
Technology Expert
Margaret Rouse
Technology Expert

Margaret é uma premiada redatora e professora conhecida por sua habilidade de explicar assuntos técnicos complexos para um público empresarial não técnico. Nos últimos vinte anos, suas definições de TI foram publicadas pela Que em uma enciclopédia de termos tecnológicos e citadas em artigos do New York Times, Time Magazine, USA Today, ZDNet, PC Magazine e Discovery Magazine. Ela ingressou na Techopedia em 2011. A ideia de Margaret de um dia divertido é ajudar os profissionais de TI e de negócios a aprenderem a falar os idiomas altamente especializados uns dos outros.