En Kuralları Of C# IList Kullanımı
Wiki Article
The following example demonstrates the implementation of the IList interface to create a simple, fixed-size list. This code is part of a larger example for the IList interface.
Bu şekilde listelerin birbirini point etmesi ve rabıtalı listenin elemanlarına valör verilmesi sağlamlanmaktadır.
Interface’ler üzerine henüz ziyade fen olmak isterseniz, dundaki kaynaklara bakış atabilirsiniz:
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 addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list
This level of abstraction goes the other direction when it belongs to method parameters. When you pass your list to a method that accepts IEnumerable you dirilik be sure that your list is not going to be modified. When you are the person implementing the method and you say you accept an IEnumerable because all you need to do is iterate through that list.
Bu şehir, istenmeyenleri azaltmak yürekin Akismet kullanıyor. Değerlendirme verilerinizin nasıl anlayışlendiği için elan ziyade haber edinin.
Don't you know in advance if your method needs a list that dirilik take additional members; don't you specify that in the method signature? What exactly were you going to do if you were passed a read only list like int[]?
In most cases, if you are using a List and you think you could C# IList Kullanımı use a narrower interface instead - why not IEnumerable? This is often a better fit if you don't need to add items. If you need to add to the collection, use the concrete type, List.
You would because defining an IList or an ICollection would open up for other implementations of your interfaces.
This argument only works if you write your own implementation of C# IList Nasıl Kullanılır IList from sratch (or at least without inheriting List)
Brad LeachBrad Leach 17k1818 gold badges7373 silver badges8888 bronze badges 1 3 It will create C# IList Nedir a new enumerable, which may hamiş be desirable in some scenarios. You cannot sort an C# IList Neden Kullanmalıyız IList in-place through the interface except by using ArrayList.Adapter method in my knowledge.
If you use a C# IList Neden Kullanmalıyız concrete implementation of list, another implementation of the same list will hamiş be supported by your code.
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.