Açıklaması C# IList Neden Kullanmalıyız Hakkında 5 Basit Tablolar

Else use List. You güç't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List hamiş an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S"

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.

If someone passes you an Array or a List, your code will work fine if you check the flag every time and have a fallback... But really; who does that?

All concepts are basically stated in most of the answers above regarding why use interface over concrete implementations.

Your code is now broken, because int[] implements IList, but is of fixed size. The contract for ICollection (the base of IList) requires the code that uses it to check the IsReadOnly flag before attempting to add or remove items from the collection. The contract for List does hamiş.

The Liskov Substitution Principle (simplified) states that a derived type should be C# IList Neden Kullanmalıyız able to be used in place of a base type, with no additional preconditions or postconditions.

use LINQ to C# IList Kullanımı 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, kakım shown above.

In fact, any time you are using reflection IList is more convenient than IList-of-T, since generics and reflection don't play nicely together. It can be done, but it is a pain. Unfortunately since IList-of-T doesn't derive from IList there are cases where this hayat yapan - but it is a good 95% rule.

And, if you don't even need everything in IList you dirilik always use IEnumerable too. With çağdaş compilers and processors, I don't think there is really any speed difference, so this is more just a matter of style.

class C# IList Nedir Kisi string ad; string soyad; public string Ad get return ad; set ad = value; public string Soyad get return soyad; kaş soyad = value;

This argument only works if you write your own implementation of IList from sratch (or at least without inheriting List)

List communicates "I need to get and C# IList Nasıl Kullanılır kaş the elements of this sequence in arbitrary order and I only accept lists; I do derece accept arrays."

I have two ILists of the same type returned by NHibernate and have emerged the two IList C# IList Kullanımı into one, hence the need for sorting.

The other general reason for using interfaces is to expose the minimum amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a data object that implements IList.

Leave a Reply

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