En Kuralları Of C# IList Neden Kullanmalıyız

Wiki Article

Else use List. You güç't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List hamiş 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"

Same principle kakım before, reversed. Offer the bare asgari that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

Here's an example: I had a project once where our lists got very large, and resulting fragmentation of the large object heap was hurting performance. We replaced List with LinkedList. LinkedList does hamiş contain an array, so all of a sudden, we had almost no use of the large object heap.

It's more nuanced than that. If you are returning an IList birli part of the public interface to your library, you leave yourself interesting options to perhaps make a custom list in the future.

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

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 implementation rather than the interface. If you hayat't justify it, use the interface.

GitHub'da bizimle ortaklaşa iş konstrüksiyonn Bu yürekğin kaynağı GitHub'da bulunabilir; burada başkaca problemlerı ve çekme isteklerini oluşturup gözden geçirebilirsiniz. Henüz bir küme veri kucakin yardımda mevcut kılavuzumuzu inceleyin.

3 @phoog: Considering where Eric is coming from, it wouldn't be surprising he is more cautious about breaking changes. But it is definitely a valid point.

If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations.

If you code your own class implementing the IList interface, make sure you also implement the non-generic IList interface, or the code will fail with a class cast exception.

Yaşama you please provide me some link to a class that implements IList interface C# IList Kullanımı or provide me a code that at least implements Add and Remove methods?

Is IList a good C# IList Nerelerde Kullanılıyor fit for your organisation? If a colleague asks you to change a method signature to use IList instead of List, ask them how they'd add an element to an IList. If they don't know about IsReadOnly (and most C# IList Nedir people don't), then don't use IList. Ever.

In collections of contiguous elements, such birli lists, the elements that follow C# IList Nedir the insertion point move down to accommodate the new element.

ahead of time. Veri-binding is a classic example here; a DataSource property usually checks for IList (and IListSource, typically) and then looks at the objects to see C# IList Nedir what properties are available. It güç't use generics in this case.

Report this wiki page