One reason I use interfaces is because it increases the flexibility of the code. Let's say we got a method that takes an object of class type Account as parameter, such as:

publicvoid DoSomething(Account account) {
// Do awesome stuff here.
}

The problem with this, is that the method parameter is fixed towards an implementation of an account. This is fine if you would never need any other type of account. Take this example, which instead uses an account interface as parameter.

This solution is not fixed towards an implementation, which means that I can pass it a SuperSavingsAccount or a ExclusiveAccount (both implementing the IAccount interface) and get different behavior for each implemented account.
As always though, don't over-engineer your code if YAGNI :o)

Interfaces are an incredibly important part of .Net. As Robert Blixt has said they provide incredible flexibility through polymorphism.There are many examples where you are required to implement a particular interface in order to be able to take advantage of certain approaches. Take for example the using Statement the object supplied to the using statement must implement the IDisposable interface. See http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspxAnother example would be in WPF, if you have implemented the MVVM design pattern and you want to attach to a command you will need to implement the ICommand interface. See http://msdn.microsoft.com/en-us/library/ms752308.aspxTake WCF, you basically couldn’t use it without the use of interfaces.
The long and the short of it is they are your friend and you should be completely comfortable with them. Good luckRicky

1 comment

The WPF example for an interface makes the most sense to me. It seems to me though if you have different flavors of the same *type*, you'd want to use inheritance for different types that share common traits with the base type. e.g. savingsAccount : Account. This way, you maximize code re-use for common functions like CreateAccount(), DeleteAccount() etc. --- Vance McCorkleFeb 01, 2011

In single inheritance languages like C# and Java a class can only inherit implementation from a single base class, but it can inherit features from multiple interfaces e.g. IScalable, ISerializable, etc...

This is essential what enables us to write code that can iterate through collections of objects that are NOT of same type (class inheritance), but that share the same specification (interface inheritance).

The best pattern I can think of to illustrate above point, is the Observer pattern (http://www.dofactory.com/Patterns/PatternObserver.aspx). The Subject can notify any object that is an Observer (interface implementors) without needing the Observers to share any implementation.