C# ILIST NEDIR IçIN ADıM HARITAYA GöRE YENI ADıM

C# IList Nedir Için Adım Haritaya göre Yeni Adım

C# IList Nedir Için Adım Haritaya göre Yeni Adım

Blog Article

We needed the list indexer infrequently, so the inefficiency was hamiş a sorun. If it had been, we could have provided some other implementation of IList, perhaps as a collection of small-enough arrays, that would have been more efficiently indexable while also avoiding large objects.

Lütfen dundaki kutuya şikayetinizin detaylarını gökçe yazın. Şikayetinizi bileğerlendirildikten sonrasında size malumat vereceğiz.

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."

Then click on the bulb symbol or place the cursor on the IList and press Strg + "." You will become several implementations offered, like:

This will allow me to do generic processing on almost any array in the .Kupkuru framework, unless it uses IEnumerable and hamiş IList, which happens sometimes.

The Liskov Substitution Principle (simplified) states that a derived type should be able to be used in place of a base type, with no additional preconditions or postconditions.

I thought I'd never need to change from a List but had to later change to C# IList Nerelerde Kullanılıyor use a custom list library for the extra functionality it provided. Because I'd only returned an IList none of the people that used the library had to change their code.

 

Whether you return an Interface or a concrete type depends upon what you want to let your callers do with the object you created -- this is an API design decision, and there's no hard and fast rule. You have to weigh their ability to make full use of the object against their ability to easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).

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

However, C# IList Nedir this makes the method more fragile, as any change to the returned object type may break the calling code. In practice though, that generally isn't a major mesele.

rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked kakım helpful. However, I would add that for most developers, most of the time, the tiny difference in yetişek size and performance is not worth worrying about: C# IList Nerelerde Kullanılıyor if in doubt, just use a List.

So typically, your methods should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation kakım required.

For instance, if you return an IEnumerable, then you are limiting them to iterating -- C# IList Nerelerde Kullanılıyor they emanet't add or remove items from your object, they güç only act against the objects. If you need to expose a collection outside of a class, but don't want to let the caller change the collection, this is C# IList Nasıl Kullanılır one way of doing it. On the other hand, if you are returning an empty collection that you expect/want them to populate, then an IEnumerable is unsuitable.

Report this page