Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I understand that an interface in .Net defines a contract between the interface and a class that inherits it. Having just gotten done working on a project that made heavy use of an interface for the Data Access Layer, it got me thinking . . . whats the big deal? When I had to add a new method to the DAL, I had to create the method signature in the interface along with adding it to the class that inherited the interface, and of course the method to the DAL, thus creating "extra work". Whats the big deal about interfaces and why would I want to create extra work for myself?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
572 views
Welcome To Ask or Share your Answers For Others

1 Answer

What's the big deal about interfaces?

Once you define the contract, you can swap out implementations without worrying about breaking the rest of your code.

Consider the situation where you have poor performing code that is making use of a List<T> in .NET. If you use the hard implementation of List<T>, there's a good chance you're going to break more code by changing the implementation.

If you were using IList<T> or IEnumerable<T> you would be able to swap List<T> for LinkedList<T> (or anything implementing your chosen interface) and fix the issue in one spot rather than having to touch all of your code.

In the end...it's about about Polymorphism.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...