BENIM C# ILIST NEDEN KULLANMALıYıZ BAşLARKEN ÇALışMAK

Benim C# IList Neden Kullanmalıyız Başlarken Çalışmak

Benim C# IList Neden Kullanmalıyız Başlarken Çalışmak

Blog Article

Note that, if your API is only going to be used in foreach loops, etc, then you might want to consider just exposing IEnumerable instead.

IList is an interface so you yaşama inherit another class and still implement IList while inheriting List prevents you to do so.

lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?

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 not, copying it to an array, sorting that, clearing the IList, and re-adding the items.

Safi 4.6 (and it will likely be caught by the compiler). But there birey be more insidious cases, such birli passing a C# array birli a IList. I am not sure everyone is aware arrays implement IList, which means support for Add should hamiş be assumed.

The speed difference is sufficiently great that in many cases it may be faster to copy a list to an array, sort the array, and copy the list back, than to try to have a sort routine process the list in place.

List implements IList, and so birey be assigned to C# IList Nasıl Kullanılır the variable. There are also other types that also implement 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 C# IList Neden Kullanmalıyız some implementation of it might throw.

For collections you should aim to use IEnumerable where possible. This gives the most C# IList Kullanımı flexibility but is derece always suited.

Then later if you decide to convert the actual data store from a List to a Dictionary and expose the dictionary keys bey the actual value for the property (I have had to do exactly this C# IList Nedir before). Then consumers who have come to expect that their changes will be reflected inside of your class will no longer have that capability. That's a big sorun! If you expose the List birli an IEnumerable you güç comfortably predict that your collection is derece being modified externally. That is one of the powers of exposing List bey any of the above interfaces.

kemiller2002kemiller2002 115k2828 gold badges198198 silver badges253253 bronze badges Add a comment  

You might want to have an IOrderRepository that defines a collection of orders in either a IList or ICollection. You could then have different kinds of implementations to provide a list of orders as long birli they conform to "rules" defined by your IList or ICollection.

This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.

would C# IList Kullanımı I run into problems with this? Since could they not pass in an array(that katışıksız a fixed size)? Would it be better maybe for a concrete List?

Report this page