Jump to content

C# Programming/Interfaces

From Wikibooks, open books for an open world

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:

interface IShape
{
    double X { get; set; }
    double Y { get; set; }
    void Draw();
}

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 Draw() 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 IDrawable. 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,

class Square : IShape
{
    private double _mX, _mY;

    public void Draw() { ... }

    public double X 
    { 
        set { _mX = value; }
        get { return _mX; }  
    }

    public double Y 
    {
        set { _mY = value; }
        get { return _mY; }
    }
}

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:

class MyClass : Class1, Interface1, Interface2 { ... }

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

class MyClass 
{
    static void Main()
    {
        IShape shape = new Square();
        shape.Draw();
    }
}

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

interface IRotateable
{
    void Rotate(double theta);
}

interface IDrawable : IRotateable
{
    void Draw();
}

Additional details

[edit | edit source]

Access specifiers (i.e. private, internal, 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.