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
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…