C# IList Nerelerde Kullanılıyor Için 5-İkinci Trick

Note that, if your API is only going to be used in foreach loops, etc, then you might want to consider just exposing IEnumerable instead.

But far more importantly, if you are accepting an IList birli a parameter you'd better be careful, because IList and List do hamiş behave the same way. Despite the similarity in name, and despite sharing an interface

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

Then click on the bulb symbol or place the cursor on the IList and press Strg + "." You will become several implementations offered, like:

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.

Örneğin, aşağıdaki kodda bir IAnimal tipinde bir parametre teşhismladım ve bu bileğişkene Dog ve Cat nesneleri atadım.

The class name List may be changed in next .safi framework but the interface is never going to change as interface is contract.

class Kisi string ad; string soyad; public string Ad get return ad; set ad = value; public string Soyad get return soyad; seki soyad = value;

In most cases, if you are using a List and you think you could use a narrower interface instead - why not IEnumerable? This is often a better fit if you don't need to add items. If you need to add to the collection, use the concrete type, List.

Collaborate with us on GitHub The source for this content kişi be found on GitHub, where you hayat also create and review issues and pull requests. For more information, see our contributor guide.

By asking for more than you need, you (1) make the caller do unnecessary C# IList Nerelerde Kullanılıyor work to satisfy your unnecessary demands, and (2) communicate falsehoods to the reader. Ask only for what you're going to use. That way if the caller has a sequence, they don't need to call ToList on it to C# IList Nedir satisfy your demand.

List communicates "I need to get and grup the elements of this sequence in arbitrary order and I only accept lists; I do not accept arrays."

If you use a concrete implementation of list, another implementation of C# IList Nerelerde Kullanılıyor the same list will not be supported by your code.

IEnumerable allows you to iterate through a collection. ICollection builds on this and also allows for adding and removing items. IList also allows for accessing and modifying them C# IList Kullanımı at a specific index. By exposing the one that you expect your consumer to work with, you are free to change your implementation. List happens to implement all three of those interfaces. If you expose your property bey a List or even an IList when all you want your consumer to have is the ability to C# IList Nerelerde Kullanılıyor iterate through the collection. Then they could come to depend on the fact that they dirilik modify the list.

Leave a Reply

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