What is an interface?

Interface generally refers to an abstraction that an entity provides of itself to the outside.

In C#, interfaces are realized using the interface keyword. It is explained as follows:

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, […]

You have to separate between these two definitions:

The “logical” side

An interface’s purpose is abstraction. Or, to express it more detailed, it abstracts the real implementation from the behavior a class ought to display. Interfaces perform some kind of grouping. They unite classes that behave similar in some situations. This allows writing much more general algorithms that only rely on the behavior that’s actually needed.

The “syntactical” side

As interfaces abstract implementations, it’s quite logical that they do not allow members to provide any of it. Classes implementing an interface must provide the implementation for the interface members.

What does it actually do?

Well – let’s look at this nice interface:

publicinterface ICanSayHelloWorld
{
string SayHelloWorld();
}

If you want to use this interface with the knowledge you have about it now, you’ll run into problems. As SayHelloWorld it called that way and returns a string, it seems likely that it returns a string that can be used to greet the world. After some thinking we write the following line of code:

Also this implementation is possible: Depending on the current time, the world is greeted differently. However, if it’s three o’clock in the morning, there will occur an exception.

Which implementation is the “correct one” now?

Short answer: None of them and still all. As the plain interface is all we have, no one of them did something wrong. All behaved as the C# Compiler claims them to do; they provide an implementation for the interface member.

Conclusion

We need to specify the behavior of our method much better (meaning: more precisely). You will see in the next part how Pre- and Postconditions help us doing this.