C# IList Neden Kullanmalıyız Için 5-İkinci Trick

Wiki Article

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

Found this thread while I was looking for a solution to the exact mesele described in the original post. None of the answers met my situation entirely, however. Brody's answer was pretty close. Here is my situation and solution I found to it.

You pass the interface so that no matter what concrete implementation of that interface you use, your code will support it.

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, derece a custom one it generally won't matter, but say for example that you found List didn't support something you needed.

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.

+1 for all of us who hate it when a small app is loaded with interfaces and clicking on "find definition" takes us somewhere OTHER than C# IList Nasıl Kullanılır the source of the sıkıntı... Kişi I borrow the phrase "Architecture Astronauts"? I yaşama see it will come in handy.

Ask those people what they'd like the methods to return. Your question is fundamentally "how do I know C# IList Kullanımı what software to write?" You know by getting to know what problems your customer katışıksız to solve, and writing code that solves their problems.

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

You would because defining an IList or an ICollection C# IList Kullanımı would open up for other implementations of your interfaces.

I know that IList is the interface and List is the concrete type but I still don't 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?

Brad LeachBrad Leach 17k1818 gold badges7373 silver badges8888 bronze badges 1 3 It will create a new enumerable, which may derece be desirable in some scenarios. You cannot sort an IList in-place through the interface except C# IList Nasıl Kullanılır by using ArrayList.Adapter method in my knowledge.

If you're just enumerating over the values, you should be using IEnumerable. Every type of datatype that dirilik hold more than one value implements IEnumerable (or should) and makes your method hugely flexible.

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 wiki page