Bir Unbiased Görünüm C# IList Nasıl Kullanılır

Else use List. You sevimli'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"

Same principle as before, reversed. Offer the bare minimal that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

Here's an example: I had a project once where our lists got very large, and resulting fragmentation of the large object heap was hurting performance. We replaced List with LinkedList. LinkedList does not contain an array, so all of a sudden, we had almost no use of the large object heap.

So when writing a function or method that takes a collection, write it hamiş to take a List, but an IList, an ICollection, or IEnumerable. The generic interfaces will still work even for heterogenous lists because System.

Başkaca yukarıda anlattığımız IndexOf metodunu Temel liste üzerinden zir listelerdeki elemanlar bağırsakin kullanamazsınız. Temel liste üzerinden madun listelerin index sırasını bulabilirsiniz.

Important Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

 

IList is an Interface, hamiş a class. If you want to initialize it, you need to initialize it to a class that implements IList, depending C# IList Neden Kullanmalıyız on your specific needs internally. Usually, IList is initialized with a List.

For example, let's say you have a Person class and a Group class. A Group instance has many people, C# IList Kullanımı so a List here would make sense. When I declare the list object in Group I will use an IList C# IList Nerelerde Kullanılıyor and instantiate it birli a List.

Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys birli the actual value C# IList Nerelerde Kullanılıyor for the property (I have had to do exactly this 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 sıkıntı! If you expose the List birli an IEnumerable you yaşama comfortably predict that your collection is hamiş being modified externally. That is one of the powers of exposing List birli any of the above interfaces.

On the other hand, when returning an object out of a function, you want to give the user the richest possible grup of operations without them having to cast around. So in that case, if it's a List internally, return a copy bey a List.

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 kakım long bey they conform to "rules" defined by your IList or C# IList Nerelerde Kullanılıyor ICollection.

In particular, IList lets you use the indexer, and add/remove items; things that IEnumerable don't let you do.

Encapsulation relies on telling clients bey little about the implementation of your class as possible. If you return a concrete List, you can't then change to some other better type without forcing all of your clients to re-compile/update.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Comments on “Bir Unbiased Görünüm C# IList Nasıl Kullanılır”

Leave a Reply

Gravatar