An interface is a contract: it specifies what members (methods and properties) a class implementing the interface must have. But because it is only a contract, it has no implementations for any of its members. A class can implement zero, one or multiple interfaces.
In contrast: a class is a... well... class of objects (like in taxonomy). For example, an Animal
is a class of living things, and a Giraffe
is a class of animals. Inheritance expresses this relationship: an Giraffe
is an Animal
when Giraffe
inherits from Animal
. It can do anything an animal can do, and more. It can provide implementations for its members, and in .NET a class will inherit from exactly one other class (which is Object
unless specified otherwise).
So, if you want to express that your class adheres to one or more contracts: use interfaces. However, you cannot provide an implementation. If you want to express that your class is something, extend a base class. In that case you can provide an implementation, but you can extend only one base class.
For a concrete example:
A linked list, an array list, a stack and a dictionary have something in common: they represent a collection of elements. But their implementations are completely different. The only thing they have in common is the contract they adhere to: ICollection
. This means your classes can ask for a collection, any collection: anything that implements ICollection
, regardless of its implementation.
On the other hand: a car, a motorcycle and a truck also have something in common: they are wheeled vehicles. But they have more in common than that: they all have a motor, they all spin their tires to go forward. Essentially, they are members of the Vehicle
class of objects, and can share (part of) their implementation. However, while a Truck
may be a Vehicle
and a CargoCarrier
, you cannot express this in C#.