En iyi Tarafı C# IList Kullanımı
We needed the list indexer infrequently, so the inefficiency was derece a problem. If it had been, we could have provided some other implementation of IList, perhaps kakım a collection of small-enough arrays, that would have been more efficiently indexable while also avoiding large objects.Then the person calling the method is free to call it with any veri type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.
Elemanların Sıralı Yapkaloriı Kayırma: IList, elemanların eklenme sırasını korur. Bu özellik, bilgi yapısının sıralı olmasını ve izlenceın beklentilerine tatminkâr çallıkışmasını esenlar.
David MillsDavid Mills 2,41511 gold badge2323 silver badges2525 bronze badges 6 2 The right approach really would have been to offer an ISortableList interface (with methods to sort a portion of the list using some particular comparer), have List implement it, and have a static method which could sort any IList by checking whether it implemented ISortableList and, if not, copying it to an array, sorting that, clearing the IList, and re-adding the items.
Basically, I need to see some actual code examples of how using IList would have solved some sıkıntı over just taking List into everything.
The Liskov Substitution Principle (simplified) states that a derived type should be able to be used in place of a base type, with no additional preconditions or postconditions.
Bu şehir, istenmeyenleri azaltmak dâhilin Akismet kullanıyor. Tefsir verilerinizin nasıl maslahatlendiği üzerine henüz fazla fen edinin.
You birey look at this argument from several C# IList Kullanımı angles including the one of a purely OO approach which says to yetişek against an Interface not an implementation. With this thought, using IList follows the same principal kakım passing around and using Interfaces C# IList Nasıl Kullanılır 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 not have to change; it knows what the IList Interface contract adheres to.
If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type. Share Improve this answer Follow
Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys birli 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 C# IList Nasıl Kullanılır your class will no longer have that capability. That's a big sıkıntı! If you expose the List birli an IEnumerable you kişi comfortably predict that your collection is derece being modified externally. That is one of the powers of exposing List bey any of the above interfaces.
IList is hamiş a class; it's an interface that classes kişi implement. The interface itself is just a contract between the consumer of the class and the class itself. This line of code will work:
Bu örnekte, Student isminde bir sınıf ve StudentCollection adında CollectionBase derslikından türeyen özel bir derme derslikı oluşturduk.
This will help if you decide to change the implementation of your class later to use a C# IList Kullanımı different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.
It doesn't affect the signature of the method, and is grup in stone C# IList Neden Kullanmalıyız 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.