News & UpdatesProgrammingWeb programming StoreMy Projects

C# Tutorial – 18 – Interface

An interface is used to specify members that deriving classes must implement. They are defined with the interface keyword followed by a name and a code block. Their naming convention is to start with a capital “I” and then to have each word initially capitalized.

interface IMyInterface {}

Interface signatures

The interface code block can only contain signatures, and only those of methods, properties, indexers and events. The interface members cannot have any implementations. Instead, their bodies are replaced by semicolons. They also cannot have any access modifiers since interface members are always public.

interface IMyInterface
  // Interface method
  int GetArea();
  // Interface property
  int Area { get; set; }
  // Interface indexer
  int this[int index] { get; set; }
  // Interface event
  event System.EventHandler MyEvent;

Interface example

In the following example, an interface called IComparable is defined with a single method named Compare.

interface IComparable
  int Compare(object o);

The class Circle defined below implements this interface, by using the same notation as is used for inheritance. The Circle class then needs to define the Compare method, which for this class will return the difference between the circle radiuses. The implemented member must be public, in addition to having the same signature as the one defined in the interface.

class Circle : IComparable
  int r;
  public int Compare(object o)
    return r - (o as Circle).r;

Although a class can only inherit from one base class it may implement any number of interfaces, by specifying them in a comma separated list after any base class.

Functionality interface

IComparable demonstrates the first usage of interfaces, which is to define a specific functionality that classes can share. It allows programmers to use the interface members without having to know the actual type of a class. To illustrate, the method below takes two IComparable objects and returns the largest one. This method will work for all classes that implement the IComparable interface, regardless of their type, since the method only uses the functionality exposed through that interface.

static object Largest(IComparable a, IComparable b)
  return (a.Compare(b) > 0) ? a : b;

Class interface

A second way to use an interface is to provide an actual interface for a class, through which the class can be used. Such an interface defines the functionality that programmers using the class will need.

interface IMyClass
  void Exposed();
class MyClass : IMyClass
  public void Exposed() {}
  public void Hidden()  {}

The programmers can then view instances of the class through this interface, by enclosing the objects in variables of the interface type.

IMyInterface m = new MyClass();

This abstraction provides two benefits. First, it makes it easier for other programmers to use the class since they now only have access to the members that are relevant to them. Second, it makes the class more flexible since its implementation can change without being noticeable by other programmers using the class, as long as the interface is followed.