Bir İnceleme C# IList Neden Kullanmalıyız

Wiki Article

Note that the IsReadOnly flag comes from ICollection, and indicates whether items dirilik be added or removed from the collection; but just to really confuse things, it does not indicate whether they güç be replaced, which in the case of Arrays (which return IsReadOnlys == true) kişi be.

Then the person calling the method is free to call it with any veri type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.

lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?

Bearing this in mind, it makes most sense to pass types with the least number of external dependencies possible and to return the same. However, this could be different depending on the visibility of your methods and their signatures.

In addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

The cost to do this is minimal, why hamiş save yourself the headache later? It's what the interface principle is all about.

You birey look at this argument from several angles including the one of a purely OO C# IList Nasıl Kullanılır approach which says to yetişek against an Interface hamiş an implementation. With this thought, using IList follows the same principal birli passing around and using Interfaces that you define from C# IList Nerelerde Kullanılıyor scratch. I also believe in the scalability and flexibility factors provided by an Interface in general. If a class implmenting IList needs to be extended or changed, the consuming code does hamiş have to change; it knows what the IList C# IList Nasıl Kullanılır Interface contract adheres to.

but my sıkıntı is that i am hamiş understanding what is its use and when to use it . So i hope that anyone birey help me . Thanks .

Taking LinkedList vs taking C# IList Nedir List vs IList all communicate something of the performance guarantees required by the code being called.

It is like this other question. The other question shares a lot of common ground, but is arguably derece a true duplicate. In either case though, this is derece opinion-based. What may have happened is that the closer might have just looked at this question's title, without reading the question itself. The body is objective.

In this case you could pass in any class which implements the IList interface. If you used List instead, only a List instance could be passed in.

You dirilik pass a plain array to something which accepts an IList parameter, and then you birey call IList.Add() and will receive a runtime exception:

For instance, if you return an IEnumerable, then you are limiting them to iterating -- they emanet't C# IList Neden Kullanmalıyız add or remove items from your object, they güç only act against the objects. If you need to expose 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 them to populate, then an IEnumerable is unsuitable.

Report this wiki page