Can someone explain the exact use of interfaces in C#?
-
2They make interfacing easier. – Igor Zevaka May 11 '10 at 05:10
-
1Interesting typo of "explain" (for those of us in the US anyway) – Austin Salonen May 11 '10 at 05:12
-
1possible duplicate of [How will I know when to create an interface?](http://stackoverflow.com/questions/444245/how-will-i-know-when-to-create-an-interface) – Hans Passant May 11 '10 at 06:41
6 Answers
This has been discussed so many times here in the past that it is hard to pick any one duplicate for this question.
To save the time of repeating what has been said before, try this search, and start going through the results.
Imagine the the situation of having a factory that creates cars. You know that every vehicle has an engine and can be started, so you have the following:
interface IVehicle
{
Engine vehicleEngine { get; set; }
bool StartEngine();
}
Now, the factory makes an array of other vehicles, so for instance a truck and a normal car:
public Car : IVehicle
{
// MUST implement vehicleEngine and StartEngine:
public Engine vehicleEngine { get; set; }
public bool StartEngine()
{
// Cars needs to do xyz to start
}
public int MaxNumberOfPassenger { get; set; } // Specific to Car
}
and then:
public Truck : IVehicle
{
// MUST implement vehicleEngine and StartEngine:
public Engine vehicleEngine { get; set; }
public bool StartEngine()
{
// Trucks needs to do abc to start
}
public int MaximumLoad { get; set; } // Specific to Truck
}
This therefore forces all vehicles to implement specific members to fall under the category of a vehicle, but then can also be specialized with their own distinct members.

- 25,001
- 7
- 80
- 118
-
Here y should i sign those methods in interface and then implementing it in my class ??? i can straight away do that in my class itself rather than using interface ?? – user337911 May 11 '10 at 05:33
-
1So that you can pass around either a Car or a Truck, when it is not important which one you pass around. TestTheVehiclesEngine(IVehice theVehicle) { theVehicle.StartEngine(); } - doesn't matter if it is a Car or a Truck because both implement IVehicle and thus both have the method StartEngine. Look up Polymorphism – Michael Shimmins May 11 '10 at 05:48
-
Another reason for using interfaces, is that it allows you to decouple your design from your implimentation and makes your code testable. If your classes ask for an interface in their constructor, instead of a concrete implimentation, you can you can swap out your concrete class for a mock. – Bayard Randel May 11 '10 at 21:06
In the most simple terms, an Interface expresses what one, or more classes can do, although the implimentation may vary across the various classes.

- 9,930
- 3
- 42
- 46
Polymorphism You can use 2 classes that implement the same interface without having to know exactly which concrete class it is. It aids in keeping code loosely coupled.

- 5,054
- 2
- 19
- 17
An interface defines the minimum requirements that a class that can be instantiated must implement. It expresses this through methods.
For instance, an interface could define a function called Foo which takes an integer and returns a boolean:
public interface ICanFoo
{
bool Foo(int number);
}
Any class which implements this interface must also implement this method:
public class Fooable : ICanFoo
{
public bool Foo(int number)
{
// do something
}
}
The implementation within the method is up to the specific classes which are implementing the interface.
By using interfaces you no longer care about implementation are compile time, but rather specification. You can call it like this:
ICanFoo myFooable = ...
bool success = fooable.Foo(4);
The actual type of fooable can be any class that implements ICanFoo since you know that ICanFoo will always define a method implementation for the Foo method.

- 19,961
- 7
- 57
- 90