C# IList Nasıl Kullanılır Herkes İçin Eğlenceli Olabilir

Wiki Article

I toyed with writing an extension method, also with inheriting from IList and implementing my own Sort() method bey well bey casting to a List but none of these seemed overly elegant.

1 @supercat: What could ISortableList offer that's not already in IList? Or, asked differently, why couldn't an IList be sorted in-place without re-adding the items by your imagined static method?

Interface’ler karşı elan şu denli marifet peyda etmek isterseniz, hordaki kaynaklara kaynak atabilirsiniz:

It's more nuanced than that. If you are returning an IList birli part of the public interface to your library, you leave yourself interesting options to perhaps make a custom list in the future.

In this specific case since you're essentially talking about a language construct, hamiş a custom one it generally won't matter, but say for example that you found List didn't support something you needed.

GitHub'da bizimle ortaklık dokuman Bu hapishaneğin kaynağı GitHub'da bulunabilir; burada ayrıca sorunları ve çekme isteklerini oluşturup gözden geçirebilirsiniz. Daha bir tomar bili için katkıda mevcut kılavuzumuzu inceleyin.

use LINQ to perform the conversion of your existing List when you return it - but it would be better to just create a more appropriate type to start with, birli shown above.

You güç look at this argument from several angles including the one of a purely OO approach which says to program against an Interface derece an implementation. With this thought, using IList follows the same principal kakım 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 changed, the consuming code does hamiş have to change; it knows what the IList Interface contract adheres to.

; being aware of the definition of the interface ie. all abstract methods that are there to be implemented by any class inheriting the interface. so if some one makes a huge class of his own with several methods besides the ones he inherited from the interface for some addition functionality, and those are of no use to you, its better to use a reference to a subclass (in this case the interface) and assign the concrete class object to it.

Then later if you decide to convert the actual data store from a List to a Dictionary and expose the C# IList Neden Kullanmalıyız dictionary keys bey the actual value for the property (I have had to do exactly this before). Then consumers who have come to expect that their changes will be reflected inside of your class will no longer have that capability. That's a big mesele! If you expose the List bey an IEnumerable you emanet comfortably predict that your collection is not being modified externally. That is one of the powers of exposing List as any of the above interfaces.

If you're working within a single method (or even in a single class or assembly in some cases) and no one outside is going to see what you're doing, use the fullness of a List. C# IList Nerelerde Kullanılıyor But if you're interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward.

Then I looked in my view(mvc) and found that I actually needed the count method birli I needed to use a for loop. So in my own application I under C# IList Nedir estimated what I actually needed how do you anticipate what someone C# IList Nasıl Kullanılır else will need or hamiş need.

Unless you have a very compelling reason to C# IList Nedir do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection since it başmaklık everything you need.

It doesn't affect the signature of the method, and is grup in stone at compile time. You should instead be helping him get over his confusion about declaring his local like IList foo = new List - this is where his confusion clearly lies.

Report this wiki page