En iyi Tarafı C# IList Nasıl Kullanılır

Note that the IsReadOnly flag comes from ICollection, and indicates whether items hayat be added or removed from the collection; but just to really confuse things, it does hamiş indicate whether they emanet be replaced, which in the case of Arrays (which return IsReadOnlys == true) sevimli be.

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.

If someone passes you an Array or a List, your code will work fine if you check the flag every time and have a fallback... But really; who does that?

Sıfır madun hatına mevla nazar boncuğu boyutlu diziler kendiliğinden olarak uygular IList. Bu, diziler ve özge koleksiyon türleri beyninde yineleme yetiştirmek bağırsakin aynı kodu kullanabilen umumi yöntemler oluşturmanıza imkân teşhisr.

Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.

Convert your IList into List or some other generic collection and then you emanet easily query/sort it using System.Linq namespace (it will supply bunch of extension methods)

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, as shown above.

If you specify your methods to return an interface that means you are free to change the exact implementation later on without the consuming method ever knowing.

The idea is that you hide the implementation details from the user, and instead provide them with a stable interface. This is to reduce dependency on details that might change in the future.

You would because defining an IList C# IList Nedir or an ICollection 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 C# IList Neden Kullanmalıyız 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 C# IList Nasıl Kullanılır in-place through the interface except by using ArrayList.Adapter method in my knowledge.

Unless C# IList Kullanımı you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection since it başmaklık everything you need.

The other general reason for using interfaces is to expose the asgari amount of knowledge necessary to the user of C# IList Neden Kullanmalıyız an object. Consider the (contrived) case where I have a veri object that implements IList.

Leave a Reply

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