C Sharp Programming/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 data members be defined. So, an interface is nothing but a collection of method and property declarations. The following defines a simple interface:

A CONVENTION            used in the .NET Framework (and likewise by many C# programmers) is to place an "I" at the beginning of an interface name to distinguish it from a class name. Another common interface naming convention is used when an interface declares only one key method, such as  in the above example. The interface name is then formed as an adjective by adding the "...able" suffix. So, the interface name above could also be. This convention is used throughout the .NET Framework.

Implementing an interface is simply done by inheriting off it and defining all the methods and properties declared by the interface after that. For instance,

Although a class can inherit from one class only, it can inherit from any number of interfaces. This is a simplified form of multiple inheritance supported by C#. When inheriting from a class and one or more interfaces, the base class should be provided first in the inheritance list, followed by any interfaces to be implemented. For example:

Object references can be declared using an interface type. For instance, using the previous examples,

Interfaces can inherit off of any number of other interfaces, but cannot inherit from classes. For example:

Additional details
Access specifiers (i.e., , etc.) cannot be provided for interface members, as all members are public by default. A class implementing an interface must define all the members declared by the interface. The implementing class has the option of making an implemented method virtual, if it is expected to be overridden in a child class.

There are no static methods within an interface, but any static methods can be implemented in a class that manages objects using it.

In addition to methods and properties, interfaces can declare events and indexers as well.

For those familiar with Java, C#'s interfaces are extremely similar to Java's.