BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Interfaces

The twenty-first part of the C# Object-Oriented Programming tutorial completes the investigation of polymorphism in the C# programming language. This instalment describes the use of interfaces to determine required members in classes that implement them.

What is an Interface?

Previously in the C# Object Oriented Programming tutorial the term interface has been used to describe the public interface of a class. The public interface contains methods, properties, indexers and other items that can be accessed by other classes. In this article, a second, related definition of interface is considered.

An interface is a code structure that is similar to an abstract class that has no concrete members. An interface can contain public members such as properties and methods but these members must have no functionality. Instead, like abstract members, they define items that must be made concrete within all classes that implement the interface. This means that an interface can be used to define what a class must do, but not how it will achieve it.

Many classes may implement a single interface. This increases polymorphism by enabling classes to behave in many different ways depending upon their usage. For example, a Customer class, an Employee class and a Business class may all implement the IWriteable interface. If this interface contains properties describing an address, a method that accepts an IWriteable object as a parameter can be used to send a letter to any of these entity types. NB: The "I" prefix for an interface is a recognised naming convention.

Unlike with the single inheritance provided by C#, a class may implement many interfaces. These may be implemented in addition to inheritance from a base class. This combination provides a simplified variation upon the multiple inheritance concept that is not available to .NET programmers.

When to Choose an Interface or Abstract Class

Interfaces and abstract classes have similar purposes. Generally, an interface should be used when the implementation of the members it defines is not fixed, or when it is known that a single class requires the contents of several interfaces. Where the functionality of some members is fixed and the limited multiple inheritance facility is not required, abstract classes may be more appropriate.

Creating Interfaces

To demonstrate the use of interfaces we will create several and apply them to a group of classes. In this article we will define interfaces and classes that represent predators and prey animals. Each animal class will implement at least one of the IPredator and IPrey interfaces.

To begin, create a new console application named "InterfacesDemo".

Declaring an Interface

An interface is declared in a similar manner to a class. The definition of the interface appears in a namespace, or within the default namespace. The interface's name is prefixed with the interface keyword and requires a code block to contain the members that it defines.

interface interface-name {}

The first interface that we will create will be implemented by all animals that are prey to predators. To use the convention for interface naming, this interface will be called "IPrey". To create the interface, add a new interface file to the project named "IPrey". If your preferred development environment does not include a template for interfaces, simply add a class file and adjust the declaration of the class to match that shown below:

interface IPrey
{
}

Creating an Interface Property

When a property is added to an interface it acts as a placeholder only, containing no functionality of its own. The functionality is added by the individual classes that implement the interface. Interface property declarations are therefore comparable to abstract property declarations and use a similar get and set accessor syntax. However, as all members of interfaces are public by definition, the "public" keyword is omitted.

The following code shows examples of read-write, read-only and write-only interface properties:

int ReadWriteProperty { get; set; }
int ReadOnlyProperty { get; }
int WriteOnlyProperty { set; }

The IPrey interface will define a property to hold the fleeing speed of prey animals. This is an important value when an animal must run away from a predator. To create the property, add the following code to the IPrey interface:

int FleeSpeed { get; set; }

Creating an Interface Method

Methods may be created within interfaces. As with properties, the method must not include an access specifier or a code block. Otherwise, the declaration uses the same syntax as a standard method.

The IPrey interface will include a method called when the animal attempts to escape from a predator. This method will be named "Flee" and will require no parameters. To create it, add the following code to the interface:

void Flee();
15 April 2008