Değil Hakkında Detaylar bilinen C# IList Kullanımı
Değil Hakkında Detaylar bilinen C# IList Kullanımı
Blog Article
We needed the list indexer infrequently, so the inefficiency was not a sorun. If it had been, we could have provided some other implementation of IList, perhaps birli a collection of small-enough arrays, that would have been more efficiently indexable while also avoiding large objects.
Then the person calling the method is free to call it with any veri type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.
Say I have a function that returns IEnumerable, inside the function I may use a List for an internal backing store to generate my collection, but I only want callers to enumerate it's contents, derece add or remove. Accepting an interface as a parameter communicates a similar message "I need a collection of strings, don't worry though, I won't change it."
Your functions above only care about being able to iterate over a list. Ideally they shouldn't need to know who implements that list or how they implement it.
Typically, a good approach is to use IList in your public facing API (when appropriate, and list semantics are needed), and then List internally to implement the API. This allows you to change to a different implementation of IList without breaking code that uses your class.
I would turn the question around a bit, instead of justifying why you should use the interface over the concrete implementation, try to justify why you would use the concrete C# IList Nasıl Kullanılır implementation rather than the interface. If you emanet't justify it, use the interface.
I thought I'd never need to change from a List but had to later change to use a custom list library for the extra functionality it provided. Because I'd only returned an IList none of the people that used C# IList Nedir the library had to change their code.
If the collection is indexed, the C# IList Kullanımı indexes of the elements that are moved are also updated. This behavior does derece apply to collections where elements are conceptually grouped into buckets, such kakım a hash table.
In most cases, if you are using a List and you think you could use a narrower interface instead - why hamiş IEnumerable? This is often a better fit if you don't need to add items. If you need to add to the collection, use the concrete type, List.
Why does the Clausius inequality involve a single term/integral if we consider a body interacting with multiple heat sources/sinks?
kemiller2002kemiller2002 115k2828 gold badges198198 silver badges253253 bronze badges Add a comment
Less ridiculous C# IList Kullanımı way to prove that an Ascii character compares equal with itself in Coq more hot questions
Dizin tarafından erişilebilen nesnelerin mutlak olarak belirlenmiş bir listesini simgeleme eder. Listeleri taharri, sıralama ve işleme şekilleri sağlar.
Encapsulation relies on telling clients kakım little about the implementation of your class as possible. If you return a concrete List, you can't then change to some other better type without C# IList Neden Kullanmalıyız forcing all of your clients to re-compile/update.