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

后端 未结 10 1207
小鲜肉
小鲜肉 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 18:48

    One Simple Answer: Use interfaces to program against the contract rather than the implementation.

    How could that possibly help? Starting to use interfaces will (hopefully) get you in the habit of coupling classes more loosely. When you code against your own concrete classes, it's easy to start poking the data structures without a strict separation of concerns. You end up with classes which "know" everything about the other classes and things can get pretty tangled. By limiting yourself to an interface, you only have the assurance that it fulfills the interface's contract. It injects a sometimes helpful friction against tight coupling.

提交回复
热议问题