C# IList Nedir Temel Açıklaması

You cannot predict the future. Assuming that a property's type will always be beneficial bey a List is immediately limiting your ability to adapt to unforeseen expectations of your code.

Lütfen kötüdaki kutuya şikayetinizin bilgilerinı gökçe yazın. Şikayetinizi bileğerlendirildikten sonrasında size veri vereceğiz.

Edit: You do need to be quick to get answers in here. Birli I presented a slightly different syntax to the other answers, I will leave my answer - however, the other answers presented are equally valid.

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.

Kemiksiz 4.6 (and it will likely be caught by the compiler). But there yaşama be more insidious cases, such birli passing a C# array birli a IList. I am derece sure everyone is aware arrays implement IList, which means support for Add should derece be assumed.

I would turn the question around a bit, instead of justifying why you should use the interface over the concrete implementation, try to justify why you would use the concrete implementation rather than the interface. If you yaşama't justify it, use the interface.

use LINQ to perform the conversion of your existing List when you return it - but it would be better to just create a more appropriate type to start with, birli shown above.

Don't you know in advance if your method needs a list C# IList Neden Kullanmalıyız that birey take additional members; don't you specify that in the method signature? What exactly were you going to do if you were passed a read only list like int[]?

the method, it sevimli make a huge difference C# IList Nedir -- if they have an array and you're asking for a list, they have to change the array to a list or v.v. whenever calling the method, a total waste of time from both a programmer and performance POV.

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

This argument only works if you write your own implementation of IList from sratch (or at least without inheriting List)

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 by using C# IList Nasıl Kullanılır ArrayList.Adapter method in my knowledge.

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 the return values.

For instance, if you return an IEnumerable, then you are limiting them to iterating -- they kişi't add or remove items from your object, they güç only C# IList Nasıl Kullanılır act 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 C# IList Kullanımı 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 *