C# IList Kullanımı - Genel Bakış

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.

Arec BarrwinArec Barrwin 61.8k99 gold badges3030 silver badges2525 bronze badges 14 71 I have to disagree with your sardonic answer. I don't totally disagree with the sentiment of over-architecture being a real mesele. However I think that especially in the case of collections that interfaces really shine.

Elemanların Sıralı Yapısını Sıyanet: IList, elemanların eklenme sırasını korur. Bu özellik, bilgi yapısının sıralı olmasını ve programın beklentilerine munis çalışmasını katkısızlar.

Your functions above only care about being able to iterate over a list. Ideally they shouldn't need to know who implements that list or how they implement it.

This will allow me to do generic processing on almost any array in the .Kupkuru framework, unless it uses IEnumerable and hamiş IList, which happens sometimes.

so its safety for you and freedom to the coder who is writing concrete implementation to change or add more functionality to his concrete class.

class Kisi string ad; string soyad; public string Ad get return ad; takım ad = value; public string Soyad get return soyad; seki soyad = value;

If your methods form part of an interface, the methods will need to be defined using types available to C# IList Kullanımı that interface.

Whether you return an Interface or a concrete type depends upon what you want to let your callers do with the object you created -- this is an API design decision, and there's no hard and fast rule. You have to weigh their ability to make full use of the object against their ability to easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).

Collaborate with us on GitHub The source for this content güç be found on GitHub, where you hayat also create and review issues and pull requests. For more information, see our contributor guide.

On the other hand, when returning an object out of a function, you want to give the user the richest possible kaş of operations without them having to cast around. So in that case, if it's C# IList Neden Kullanmalıyız 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 as they conform to "rules" C# IList Nasıl Kullanılır defined by your IList or ICollection.

for your return types. This gives your callers the most flexibility in passing in types to your methods and the most opportunities to cast/reuse C# IList Neden Kullanmalıyız the return values.

For instance, if you return an IEnumerable, then you are limiting them to iterating -- they yaşama't add or remove items from your object, they can only act C# IList Neden Kullanmalıyız against the objects. If you need to expose a collection outside of a class, 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.

Leave a Reply

Your email address will not be published. Required fields are marked *