C# ILIST NEDEN KULLANMALıYıZ SEçENEKLER

C# IList Neden Kullanmalıyız Seçenekler

C# IList Neden Kullanmalıyız Seçenekler

Blog Article

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

Arec BarrwinArec Barrwin 61.8k99 gold badges3030 silver badges2525 bronze badges 14 71 I have to disagree with your sardonic answer. I don't totally disagree with the sentiment of over-architecture being a real sorun. However I think that especially in the case of collections that interfaces really shine.

I think that, ideally, the .NET Framework would include a static sorting method that accepts an IList, but the next best thing is to create your own extension method. It's not too hard to create a couple of methods that will allow you to sort an IList as you would a List.

David MillsDavid Mills 2,41511 gold badge2323 silver badges2525 bronze badges 6 2 The right approach really would have been to offer an ISortableList interface (with methods to sort a portion of the list using some particular comparer), have List implement it, and have a static method which could sort any IList by checking whether it implemented ISortableList and, if hamiş, copying it to an array, sorting that, clearing the IList, and re-adding the items.

Use whatever you want. C# IList Neden Kullanmalıyız It's your method. You're the only one who gets to see the internal implementation details of the method.

List is a specific implementation of IList, which is a container that kişi be addressed the same way bey a linear array T[] C# IList Kullanımı using an integer index. When you specify IList kakım the type of the method's argument, you only specify that you need certain capabilities of the container. For example, the interface specification does not enforce a specific veri structure to C# IList Nerelerde Kullanılıyor be used.

In case of using IList, the caller is always guareented things to work, and the implementer is free to change the underlying collection to any alternative concrete implementation of IList

Matthew WatsonMatthew Watson 108k1111 gold badges170170 silver badges290290 bronze badges 2 2 This is trivially true for every interface. If you want to follow through with your argument, than you could argue to never use any interface at all, because some implementation of it might throw.

In most cases, if you are using a List and you think you could use a narrower interface instead - why not IEnumerable? This is often a better fit if you C# IList Nasıl Kullanılır don't need to add items. If you need to add to the collection, use the concrete type, List.

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

 

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: .

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

I read a lot of posts saying how this makes it easier to change the implementation later on, but I just don't fully see how that works.

Report this page