C# IList Neden Kullanmalıyız Için 5-İkinci Trick
Wiki Article
Note that, if your API is only going to be used in foreach loops, etc, then you might want to consider just exposing IEnumerable instead.
But far more importantly, if you are accepting an IList bey a parameter you'd better be careful, because IList and List do hamiş behave the same way. Despite the similarity in name, and despite sharing an interface
lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?
Kendi derme sınıflarınızı oluştururken baştan kullanılabilir kod yazmanızı sağlar: C# CollectionBase kullanarak umumi koleksiyon mesleklemlerini dâhilaziz bir baz dershane oluşturabilirsiniz.
This will allow me to do generic processing on almost any array in the .Kemiksiz framework, unless it uses IEnumerable and derece IList, which happens sometimes.
You yaşama have an instance of an interface, but you need to initialize it with a class that implements that interface such birli:
StuartLCStuartLC 106k1818 gold badges216216 silver badges289289 bronze badges Add a comment
Ask those people what they'd like the methods to return. Your question is fundamentally "how do I know what software to write?" You know by getting to know what problems your customer has to solve, and writing code that solves their problems.
In most cases, if you are using a List and you think you could 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, C# IList Neden Kullanmalıyız List.
Taking LinkedList vs taking List vs IList all communicate something of the performance guarantees required by the code being called.
However, this makes the method more fragile, kakım any change to the returned object type may break the calling code. In practice though, that generally isn't C# IList Neden Kullanmalıyız a major mesele.
rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked birli helpful. However, I would add that for most developers, most of the time, C# IList Kullanımı the tiny difference in program size and performance C# IList Nasıl Kullanılır is not worth worrying about: if in doubt, just use a List.
for your return types. This gives your callers the most flexibility in passing in types to your methods C# IList Kullanımı and the most opportunities to cast/reuse the return values.
The other general reason for using interfaces is to expose the minimal amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a veri object that implements IList.