Açıklaması C# IList Nasıl Kullanılır Hakkında 5 Basit Tablolar

Wiki Article

Else use List. You güç't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List derece 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"

IList is an interface so you güç inherit another class and still implement IList while inheriting List prevents you to do so.

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.

It's more nuanced than that. If you are returning an IList kakım part of the public interface to your library, you leave yourself interesting options to perhaps make a custom list in the future.

Typically, a good approach is to use IList in your public facing API (when appropriate, and list semantics are needed), and then List internally to implement the API. This allows you to change to a different implementation of IList without breaking code that uses your class.

GitHub'da bizimle ortaklık dokuman Bu gönülğin kaynağı GitHub'da bulunabilir; burada hassaten sorunları ve çekme isteklerini oluşturup gözden geçirebilirsiniz. Elan zait haber sinein katkıda kâin kılavuzumuzu inceleyin.

 

If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does hamiş apply to collections where elements are conceptually grouped into buckets, such birli a hash table.

For example, let's say you have a Person class and a Group class. A Group instance has many people, so a List here would make sense. When I declare the list object in Group I will use an IList and instantiate it bey a List.

C# IList Kullanımı Modülerlik: Yazılım projelerinde modüler bir yaklaşım sunarak harf tekrarını azaltır ve bakımı kolaylaştırır.

IList is derece a class; it's an interface that classes can implement. The interface itself is just a contract between the consumer of the class and the class itself. This line of code will work:

This is usually guaranteed for a specific implementation of a container (List documentation: "It C# IList Neden Kullanmalıyız implements the IList generic interface using an array whose size is dynamically increased as required.").

When talking about return types, the more specific you are, the more flexible callers emanet be with it.

For instance, if you return an IEnumerable, then you are limiting them to iterating -- they kişi't add or remove C# IList Nerelerde Kullanılıyor items from your object, they sevimli only act against the objects. If you need to expose C# IList Nedir 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 C# IList Nerelerde Kullanılıyor them to populate, then an IEnumerable is unsuitable.

Report this wiki page