Açıklanan C# IList Nerelerde Kullanılıyor Hakkında 5 Kolay Gerçekler

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.

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.

Bir dahaki sefere yorum yapmış olduğumda kullanılmak üzere adımı, e-posta adresimi ve web şehir adresimi bu tarayıcıya kaydet.

So when writing a function or method that takes a collection, write it derece to take a List, but an IList, an ICollection, or IEnumerable. The generic interfaces will still work even for heterogenous lists because System.

The accepted answer by @DavidMills is quite good, but I think it yaşama be improved upon. For one, there is no need to define the ComparisonComparer class when the framework already includes a static method Comparer.Create(Comparison). This method birey be used to create an IComparison on the fly.

The speed difference is sufficiently great that in many cases it may be faster to copy a list to an array, sort the array, and copy the list back, than to try to have a sort routine process the list in place.

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

You dirilik look at this argument from several angles C# IList Neden Kullanmalıyız including the one of a purely OO 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 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 C# IList Neden Kullanmalıyız changed, the consuming code does not have to change; it knows what the IList Interface contract adheres to.

For example, let's say you have a Person class and a Group class. A Group instance saf many people, C# IList Kullanımı so a List here would make sense. When I declare the list object in Group I will use C# IList Nerelerde Kullanılıyor an IList and instantiate it bey a List.

tranmqtranmq 15.5k33 gold badges3232 silver badges2727 bronze badges 6 But in C# IList Nasıl Kullanılır this case I birey't bind my collection to DataGridView rather I have to expose the _list member in MyCollection.

Hordaki şekilde Kisi adında oluşturduğumuz dershaneı oluşturduğumuz liste nesnesine ekleyelim.

Benefit of using an Interface is that you get to implement your functionality or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no need for the Sort, Add methods.

You yaşama pass a plain array to something which accepts an IList parameter, and then you kişi call IList.Add() and will receive a runtime exception:

If you had used IList in the rest of the app you could extend List with your own custom class and still be able to pass that around without refactoring.

Leave a Reply

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