Düşünceler Hakkında Bilmek C# IList Nedir

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

Başarım Optimizasyonu: IList, veri erişimini optimize ederek uygulamanın performansını pozitifrabilir ve hafıza yönetimini iyileştirebilir.

Say I have a function that returns IEnumerable, inside the function I may use a List for an internal backing store to generate my collection, but I only want callers to enumerate it's contents, derece add or remove. Accepting an interface bey a parameter communicates a similar message "I need a collection of strings, don't worry though, I won't change it."

Eric LippertEric Lippert 656k182182 gold badges1.3k1.3k silver badges2.1k2.1k bronze badges 14 12 How do you know what the caller needs though. For instance I was switching one of my return types to a IList then I well I am probably just going to enumerate over them anyways lets just return an IEnumberable.

üste mafevkda anlattığımız IndexOf metodunu Esas liste üzerinden alt listelerdeki elemanlar kucakin kullanamazsınız. Anne liste üzerinden zir listelerin index sırasını bulabilirsiniz.

Architecture Astronauts. The chances you will ever write your own IList that adds anything to the ones already in the .NET framework are so remote that it's theoretical jelly tots reserved for "best practices".

List implements IList, and so güç be assigned to the variable. There are also other types that also implement IList.

If you specify your methods to return an interface that means you are free to change the exact implementation later on without the consuming method ever C# IList Nedir knowing.

If the parameter type is IList, then the caller has much more freedom, and hayat use classes you never heard about, which may not even have existed when your code was written.

If you kişi consider your method, determine that you probably won't be changing the return collection type, then it is probably safe to return a more exact type. If you aren't sure, or are afraid that if you change it in future you'll be breaking other people's code, then go more general.

It is C# IList Neden Kullanmalıyız like C# IList Nerelerde Kullanılıyor this other question. The other question shares a lot of common ground, but is arguably hamiş a true duplicate. In either case though, this is not opinion-based. What may have happened is that the closer might have just looked at this question's title, C# IList Nerelerde Kullanılıyor 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.

And, if you used a generic implementation, you would only be able to use a method that works for any object only with objects of a specific type.

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 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 kakım a List or even an IList when all you want your C# IList Nerelerde Kullanılıyor consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact that they birey modify the list.

Leave a Reply

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