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

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

.net Interface explanation

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...