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

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

Bu şekilde listelerin birbirini point etmesi ve ilişkilı listenin elemanlarına fehamet verilmesi esenlanmaktadır.

Bu site, istenmeyenleri azaltmak yürekin Akismet kullanıyor. Tefsir verilerinizin nasıl emeklendiği için henüz zait bilgi edinin.

Sıfır ast hatına ehil hiç boyutlu diziler kendiliğinden olarak uygular IList. Bu, diziler ve öbür koleksiyon türleri arasında yineleme salgılamak dâhilin aynı kodu kullanabilen genel yöntemler oluşturmanıza imkân tanır.

Basically, I need to see some actual code examples of how using IList would have solved some problem over just taking List into everything.

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.

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.

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 Nasıl Kullanılır on your specific needs internally. Usually, IList is initialized with a List.

In most cases, if you are using a List and you think you could use a narrower interface instead - why derece IEnumerable? This is C# IList Nasıl Kullanılır often a better fit if you don't need to add items. If you need to add to the C# IList Kullanımı collection, use the concrete type, List.

The Cast function is just a reimplementation of the extension method that comes with 3.5 written as a düzgülü static method. It is quite ugly and verbose unfortunately.

If you're working within a single method (or even in a single class or assembly in some cases) and no one outside C# IList Nerelerde Kullanılıyor is going to see what you're doing, use the fullness of a List. But if you're C# IList Nerelerde Kullanılıyor interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward.

Brad LeachBrad Leach 17k1818 gold badges7373 silver badges8888 bronze badges 1 3 It will create a new enumerable, which may hamiş be desirable in some scenarios. You cannot sort an IList in-place through the interface except by using ArrayList.Adapter method in my knowledge.

Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection since it özgü everything you need.

If you had used IList in the rest of the app you could extend List with your own custom class and still be able to pass that around without refactoring.

Leave a Reply

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