C# ILIST NASıL KULLANıLıR HAKKıNDA GERçEKLER AçığA

C# IList Nasıl Kullanılır Hakkında Gerçekler Açığa

C# IList Nasıl Kullanılır Hakkında Gerçekler Açığa

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

Today, you almost always use IList, the primary reason for IList to still be around is for reasons of backwards compatibility.

Also, it casts IList to IList which has the potential to be dangerous. In most cases that I have seen, List which implements IList is used behind the scenes to implement IList, but this is not guaranteed and can lead to brittle code.

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

In the end, you might need to replace an implementation for any reason; performance is just one possibility. Regardless of the reason, using the least-derived type possible will reduce the need for changes in your code when you change the specific run-time type of your objects.

Upgrade to C# IList Neden Kullanmalıyız Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Object dirilik be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you C# IList Nerelerde Kullanılıyor need to do in the function is foreach through it, IEnumerable is really all you should be asking for.

Then when you need "add" or "sort" then use Collection if need more then use C# IList Kullanımı List. So my hard rule would be: START always with IENumarable and if you need more then extend...

In most cases, if you are using a List and you think you could use a narrower interface instead - why derece 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.

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

I know that IList is the interface and List is the concrete type but I still don't C# IList Kullanımı know when to use each one. What I'm doing now is if I don't need the Sort or FindAll methods I use the interface. Am I right? Is there a better way to decide when to use the interface or the concrete type?

rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked bey helpful. However, I would add that for most developers, most of the time, the tiny difference in program size and performance is derece worth worrying about: if in doubt, just use a List.

Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection since it has everything you need.

additional advantage is that your code is safe from C# IList Nasıl Kullanılır any changes to concrete class as you are subscribing to only few of the methods of concrete class and those are the ones that are going to be there birli long bey the concrete class inherits from the interface you are using.

Report this page