开发者

interface concept in C# [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What are some advantages to using an interface in C#?

Why we are using开发者_Python百科 interface in C#. Whats the advantage for using interface.Can you please explain about this concept to understand easily and also simple example for interface ?


Interfaces are a way to separate... well the interface from the implementation.

You can say here is a function that gives me the n-th prime number. That's the interface. If I am a user I don't care how you compute it - so I only need the interface.

Also interfaces provide an alternative for the multiple inheritance - you can have only one base class, but can implement multiple interfaces.

Example:

interface IPrimeComputer {
    int GetNPrime(int n);
}

class SieveComputer : IPrimeComputer {
    public int GetPrime(int n) {
        //compute the nth prime using a sieve
    }
}

class MagicComputer : IPrimeComputer {
    public int GetPrime(int n) {
        //compute the nth prime using some magic that no one else can understand.
    }
}

...
 IPrimeComputer computer = new MagicComputer();

 int nprime = computer.GetPrime(100);

 computer = new SieveComputer();

 nprime = computer.GetPrime(100);


Implementing an interface says HEY! LOOK AT ME! I CAN DO THIS.

Interfaces lend themselves toward many applications:

  • Unit testing: If your methods accept interfaces as parameters, you can pass mock objects to test for bugs ensuring that any bugs found are not caused by bugs in the parameters.
  • Code Reuse: You can make your code more generic if it uses interfaces, and more generic code lends itself to reuse. For example, UI Controls are highly reusable components which make use of interfaces such as INotifyPropertyChanged, which when implemented makes your custom classes work well with databinding.
  • Software engineering best practice: It's good to separate the specification from the implementation because smarter, more experienced people said so. There are also plenty of good design patterns which require the use of interfaces.
  • Mixins: Combined with extension methods you can use interfaces to implement quasi-mixins. When you implement that interface on a class, the class will inherit the relevant functionality. The best example I can think of here are the LINQ extension methods, which appply to classes which implement IEnumerable. Interfaces by themselves do not give us multiple inheritance, but mixins do.
  • Crossing system boundaries: It's often nice to create interfaces for objects that you expect to cross system boundaries (i.e. web services). This can simplify serialisation and allows you to modify the concrete class without affecting the interface between two systems.


Yes, interface touches the fact that C# not support multiple inheritance, but that is not why you should use interfaces. As Peter says it is a way of separating the implementation of the object and the capabilities of the object. The interface defines what you can do with an object (the capabilities) but the implementation of the objects defines how you do it.

Let say you are making a graphical program where you prints shapes to the screen that you want to rotate. Then you might have an interface IShape that defines the methods Rotate and Draw but the real implementation of those methods lies in the classes Rectangle, Circle etc. since you most likely want to use more than one shape.


See this SO question for advantages of interfaces What are some advantages to using an interface in C#?

A simple example of an interface might be:

public interface IAnimal
{

    void Sleep();

    void Run();

    void Talk();
}

The interface would then be implemented by a class, that would implement the methods specified in the interface:

public class Dog : IAnimal
{

    public void Talk()
    {
        Console.WriteLine("Ruff!");
    }

    public void Run()
    {
        Console.WriteLine("The dog runs after balls and cars.");
    }

    public void Sleep()
    {
        Console.WriteLine("ZZZZZZZZZZ");
    }
}

And a different class would implement the same methods differently:

public class Cat : IAnimal
{

    public void Talk()
    {
        Console.WriteLine("Meow");
    }

    public void Run()
    {
        Console.WriteLine("The cat looks at you and says, 'Yeah, right...'");
    }

    public void Sleep()
    {
        Console.WriteLine("All the time - what else does a cat do?");
    }
}

These are trivial examples, but should give you an idea.


One benefit is that the class which implements an interface is in no choice but to implement the methods of the interface on its own way.

So if you are the creator of the interface, and other programmers needs to implement it from their own classes, those classes will have the pattern that came from your interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜