TEMEL İLKELERI C# ILIST NASıL KULLANıLıR

Temel İlkeleri C# IList Nasıl Kullanılır

Temel İlkeleri C# IList Nasıl Kullanılır

Blog Article

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

Maybe take it into a List of T rather than ArrayList, so that you get type safety and more options for how you implement the comparer.

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

swilliamsswilliams 48.6k2727 gold badges101101 silver badges130130 bronze badges 3 5 why derece make it a just a List in the first place? I still don't understand why bonus you get from making it a IList then in the constructor you make it into a List

In some code this gönül be quite important and using concrete classes communicates your intent, your need for that specific class. An interface on the other hand says "I just need to call this seki of methods, no other contract implied."

Object güç 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 need to do in the function is foreach through it, IEnumerable is C# IList Nerelerde Kullanılıyor really all you should be asking for.

In fact, any time you are using reflection IList is more convenient than IList-of-T, since generics C# IList Nedir and reflection don't play nicely together. It sevimli be done, but it is a pain. Unfortunately since IList-of-T doesn't derive from IList there are cases where this sevimli işleyen - but it is C# IList Neden Kullanmalıyız a good 95% rule.

For collections you should aim to use IEnumerable where possible. This gives the most flexibility but is not always suited.

What US checks and balances prevent the FBI from C# IList Neden Kullanmalıyız raiding politicians unfavorable to the federal government? more hot questions

By asking for more than you need, you (1) make the caller do unnecessary work to satisfy your unnecessary demands, and (2) communicate falsehoods to the reader. Ask only for what you're going to use. That way if the caller katışıksız a sequence, they don't need to call ToList on it to satisfy your demand.

List communicates "I need to get and seki the elements of this sequence in arbitrary order and I only accept lists; I do not accept arrays."

I have two ILists of the same type returned by NHibernate and have emerged the two IList into one, hence the need for sorting.

For instance, if you return an IEnumerable, then you are limiting them to iterating -- they birey't add or remove items from your object, they dirilik only act against the objects. If you need to expose a collection outside of a class, C# IList Nedir but don't want to let the caller change the collection, this is one way of doing it. On the other hand, if you are returning an empty collection that you expect/want them to populate, then an IEnumerable is unsuitable.

Report this page