C# IList Neden Kullanmalıyız Seçenekler

Wiki Article

It's always best to use the lowest base type possible. This gives the implementer of your interface, or consumer of your method, the opportunity to use whatever they like behind the scenes.

Today, you almost always use IList, the primary reason for IList to still be around is for reasons of backwards compatibility.

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?

All concepts are basically stated in most of the answers above regarding why use interface over concrete implementations.

ToList first? How about returning it? I don't understand what you mean by "method will survive" when using vars? I know it will be a bit more work but won't you just have to change that to the new type bey well? So maybe IList to IList?

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 sevimli't justify it, use the interface.

Object yaşama be a T too. Doing this will C# IList Kullanımı 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 really all you should be asking for.

C# IList Nedir Then when you need "add" or "sort" then use Collection if need more then use List. So C# IList Nedir my hard rule would be: START always with IENumarable and if you need more then extend...

For collections you should aim to use IEnumerable where possible. This gives the most flexibility but is hamiş C# IList Kullanımı always suited.

tranmqtranmq 15.5k33 gold badges3232 silver badges2727 bronze badges 6 But in this case I güç't bind my collection to DataGridView rather I have to expose the _list member in MyCollection.

C# IList Kullanımı Dundaki şekilde Kisi adında oluşturduğumuz derslikı oluşturduğumuz liste nesnesine ekleyelim.

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 ArrayList.Adapter method in my knowledge.

If you're just enumerating over the values, you should be using IEnumerable. Every type of datatype that gönül hold more than one value implements IEnumerable (or should) and makes your method hugely flexible.

The other general reason for using interfaces is to expose the asgari amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a data object that implements IList.

Report this wiki page