C# IList Nasıl Kullanılır Hakkında Gerçekler Açığa

It's always best to use the lowest base type possible. This gives the implementer of your interface, or consumer of your method, the opportunity to use whatever they like behind the scenes.

Same principle birli before, reversed. Offer the bare minimal that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

Edit: You do need to be quick to get answers in here. Birli I presented a slightly different syntax to the other answers, I will leave my answer - however, the other answers presented are equally valid.

A List object allows you to create a list, add things to it, remove it, update it, index into it and etc. List is used whenever you just want a generic list where you specify object type in it and that's it.

ToList first? How about returning it? I don't understand what you mean by "method will survive" when using vars? I know it will be a bit more work but won't you just have to change that to the new type as well? So maybe IList to IList?

Örneğin, süflidaki kodda bir IAnimal tipinde bir değişebilir tanımladım ve bu değaksiyonkene Dog ve Cat nesneleri atadım.

 

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

This example also tells you that there may be situations when you need to specify the implementation, not the interface, in the argument list: In this example, whenever you require a particular access performance characteristic.

You would because defining an IList or an ICollection would open up for other implementations of your C# IList Kullanımı interfaces.

On the other hand, when returning an object out of a function, you want to give the user the richest possible seki of operations without them having to cast around. So in that case, if it's C# IList Kullanımı a List internally, return a copy as a List.

Collaborate with us on GitHub The source for this content gönül be found on GitHub, where you yaşama also create C# IList Kullanımı and review issues and pull requests. For more information, see our contributor guide.

This will help if you decide C# IList Kullanımı to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.

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 birli a List or even an IList when all you want your consumer to have is the ability to iterate C# IList Nedir through the collection. Then they could come to depend on the fact that they sevimli modify the list.

Leave a Reply

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