Should i use ilist or ienumerable
For example, a collection containing rows returned from a database query would be a snapshot. Collections that always represent the current state are called live collections.
For example, a collection of ComboBox items is a live collection. Property getters should be very lightweight operations. Returning a snapshot requires creating a copy of an internal collection in an O n operation. In general, all collections representing a shared resource e.
Such collections are very difficult or impossible to implement as live collections unless the implementation is simply a forward-only enumerator. Collections provide more control over contents, can evolve over time, and are more usable.
In addition, using arrays for read-only scenarios is discouraged because the cost of cloning the array is prohibitive. Usability studies have shown that some developers feel more comfortable using collection-based APIs. However, if you are developing low-level APIs, it might be better to use arrays for read-write scenarios.
Arrays have a smaller memory footprint, which helps reduce the working set, and access to elements in an array is faster because it is optimized by the runtime. Using IEnumerable we can find out the no of elements in the collection after iterating the collection.
IEnumerable supports deferred execution. IEnumerable supports further filtering. I hope after reading this article you will be able to boost your LINQ query performance. I would like to have feedback from my blog readers.
Please post your feedback, question, or comments about this article. Take our free skill tests to evaluate your skill! In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths. Disclaimer :. I want to know which is better to return There is no such a type that is always better to return. Tags: C Asp. Net Asp. Net Mvc Vb. Shortest Hello World program with no semi colons Update. Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu Improve this answer.
Achilles 33 5 5 bronze badges. Rob Levine Rob Levine I agree with using the least derived type. Yes: IList does implement IEnumerble: msdn. After all, a list is an enumerable collection of things that can be iterated over. To say that we should try to use the least specific type is at least not accurate.
People will ask: Why don't we use object all the time then since object is the least derived type? You've misunderstood the point I think. I'm not suggesting you always use the least derived type "full-stop". You use the least specific type that suits your purpose and has the methods and properties you need.
So, for example, system.
0コメント