C# IList Nerelerde Kullanılıyor Için 5-İkinci Trick

Wiki Article

Else use List. You birey't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List not an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S"

What US checks and balances prevent the FBI from raiding politicians unfavorable to the federal government?

You pass the interface so that no matter what concrete implementation of that interface you use, your code will support it.

Eric LippertEric Lippert 656k182182 gold badges1.3k1.3k silver badges2.1k2.1k bronze badges 14 12 How do you know what the caller needs though. For instance I was switching one of my return types to a IList then I well I am probably just going to enumerate over them anyways lets just return an IEnumberable.

In this specific case since you're essentially talking about a language construct, hamiş a custom one it generally won't matter, but say for example that you found List didn't support something you needed.

In some code this dirilik be quite important and using concrete classes communicates your intent, your C# IList Nasıl Kullanılır need for that specific class. An interface on C# IList Neden Kullanmalıyız the other hand says "I just need to call this seki of methods, no other contract implied."

Inside the method, you should use var, instead of IList or C# IList Nasıl Kullanılır List. When your data source changes to come from a method instead, your onlySomeInts method will survive.

If you specify your methods to return an interface that means you are free to change the exact implementation later on without the consuming method ever knowing.

If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type. Share Improve this answer Follow

Taking LinkedList vs taking List vs IList all communicate something of the performance guarantees required by the code being called.

Obviously if you are being asked which you use in an interview, you say IList, smile, and both look pleased at yourselves for C# IList Nerelerde Kullanılıyor being so clever. Or for a public facing API, IList. Hopefully you get my point.

The most important case for using interfaces over implementations is in the parameters to your API. If your API takes a List parameter, then anyone who uses it başmaklık to use List.

Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection since it katışıksız everything you C# IList Neden Kullanmalıyız need.

Encapsulation relies on telling clients as little about the implementation of your class kakım possible. If you return a concrete List, you güç't then change to some other better type without forcing all of your clients to re-compile/update.

Report this wiki page