En Kuralları Of C# IList Neden Kullanmalıyız

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

Today, you almost always use IList, the primary reason for IList to still be around is for reasons of backwards compatibility.

Bir dahaki sefere versiyon yapmış olduğumda kullanılmak üzere aşamaı, elektronik posta adresimi ve web site adresimi bu tarayıcıya kaydet.

Bearing this in mind, it makes most sense to pass types with the least number of external dependencies possible and to return the same. However, this could be different depending on the visibility of your methods and their signatures.

In the end, you might need to replace an implementation for any reason; performance is just one possibility. Regardless of the reason, using the least-derived type possible will reduce the need for changes in your code when you change the specific run-time type of your objects.

Örneğin, hordaki kodda bir IAnimal tipinde bir parametre tanılamamladım ve bu bileğkârkene Dog ve Cat nesneleri atadım.

 

Now I am returning IList for the simple fact that I will then add this to my domain biçim what başmaklık a property like this:

Whether you return an Interface or a concrete type depends upon what you want to let your callers do with the object you created -- this is an API design decision, and there's no hard and fast rule. You have to weigh their ability to make full use of the object against their ability C# IList Kullanımı to easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).

Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys kakım 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 kakım an IEnumerable you kişi comfortably predict that your collection is hamiş being modified externally. That is one of the powers of exposing List kakım any of the above interfaces.

However, this makes the method more fragile, as any change to the returned object type may break the calling code. In practice though, that generally isn't a major problem.

rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked kakım helpful. However, I would add that for C# IList Nedir most developers, most of the time, the tiny difference in yetişek size and performance is derece worth worrying about: if in doubt, just use a List.

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

IEnumerable allows you to iterate through a collection. ICollection builds on this and also allows for C# IList Nerelerde Kullanılıyor adding and removing items. IList also allows for accessing and modifying them at a specific index. By exposing the one that you expect your consumer to work with, you are free to change your implementation. List happens to implement all three of those interfaces. If you expose your property birli a List or C# IList Kullanımı even an IList when all you want your consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact that they emanet modify C# IList Nerelerde Kullanılıyor the list.

Leave a Reply

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