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

Wiki Article

You cannot predict the future. Assuming that a property's type will always be beneficial kakım a List is immediately limiting your ability to adapt to unforeseen expectations of your code.

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

You pass the interface so that no matter what concrete implementation of that interface you use, your code will support it.

If you think that interfaces are useful only for building over-sized, grandiose architectures and have no place in small shops, then I hope that the person sitting across from you in the interview isn't me.

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.

IList on the other hand is an Interface. Basically, if you want to create your own custom List, say a list class called BookList, then you hayat use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

Most app-level code (i.e. the everyday code that ou write for your application) should probably focus on the generic versions; however there is also a lot of infrastructure code around that must use reflection.

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 knowing.

If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations.

tranmqtranmq 15.5k33 gold badges3232 silver badges2727 bronze badges 6 But in this case I emanet't bind my collection to DataGridView rather I have to expose the _list member in MyCollection.

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

The most important case for using interfaces over implementations is in the parameters to C# IList Nedir your API. If your API takes a List parameter, then anyone who uses it katışıksız to use List.

Dizin aracılığıyla erişilebilen nesnelerin kesin olarak belirlenmiş bir listesini söz gelişi paha. Listeleri arama, sıralama ve el işi yöntemleri katkısızlar.

For instance, if you return an IEnumerable, then you are C# IList Nasıl Kullanılır limiting them to iterating -- they sevimli't add or remove items from your object, they sevimli only act C# IList Nerelerde Kullanılıyor against the objects. If you need to expose a collection outside C# IList Kullanımı 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