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

后端 未结 10 1211
小鲜肉
小鲜肉 2020-12-04 18:16

I was forced into a software project at work a few years ago, and was forced to learn C# quickly. My programming background is weak (Classic ASP).

I\'ve learned qui

10条回答
  •  一生所求
    2020-12-04 19:05

    good article.

    An interface is a contract that guarantees to a client how a class or struct will behave.

    http://www.codeguru.com/csharp/csharp/cs_syntax/interfaces/article.php/c7563

    This might be the clearest easiest way of explaining that I have come across:

    "The answer is that they provide a fairly type-safe means of building routines that accept objects when you don't know the specific type of object that will be passed ahead of time. The only thing you know about the objects that will be passed to your routine are that they have specific members that must be present for your routine to be able to work with that object. The best example I can give of the need for interfaces is in a team environment. Interfaces help define how different components talk to each other. By using an interface, you eliminate the possibility that a developer will misinterpret what members they must add to a type or how they will call another type that defines an interface. Without an interface, errors creep into the system and don't show up until runtime, when they are hard to find. With interfaces, errors in defining a type are caught immediately at compile time, where the cost is much less."

提交回复
热议问题