tags:

views:

190

answers:

7

Can some one explain the exact use of interfaces in C# ?

+2  A: 

Has msdn not been helpful on this?

http://msdn.microsoft.com/en-us/library/87d83y5b.aspx

ondesertverge
+2  A: 

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.

slugster
A: 

In the most simple terms, an Interface expresses what one, or more classes can do, although the implimentation may vary across the various classes.

Bayard Randel
MUST do, not can do.
Michael Shimmins
A: 

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.

John Buchanan
A bit more in detail would be helpful!!!
+2  A: 

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.

Kyle Rozendo
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 ??
So 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
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
A: 

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.

Michael Shimmins