c# - In which cases are IEnumerable<T>.Count optimized? -


After using "itemprop =" text ">

I have seen that method is a prerequisite for this case to optimize it when IEnumerable & Lt; T & gt; passed a really iconging & lt; T & gt; . . T & gt;

this I think is that IEnumerable Based on the & lt started the code> can be used as a readable view of the collection, without performance loss, which I originally had to do with IEnumerable & lt; T & gt;

Could be used without requiring the API's base. Optimizes calculation still when IEnumerable & LT; T & gt; Select the result of a more than a statement ICollection , but this case is not optimized based on the reflected code, and walk through all the elements Necessary.

Do you take the same conclusions from Reflection? What could be the reason behind the lack of this optimization? I feel that a lot of time has been spent in this common operation.

"text">

In fact it does not matter that select result is a sluggish evaluation calculation is always equal to the original collection count, so it's definitely a special object. > Select could be obtained directly by returning which can be used for short circuit evaluation calculation method of.

It is not possible to evaluate the calculation ( method) at the return value of the reason Select the call with the fixed number (such as The list can change the meaning of the program

Selector method to allow relevant effects And its side effects are definitely needed to be done, in a predefined order.

Assume:

  new [] {1,2,3}. Select (i = & gt; {Console.WriteLine (i); return 0;}). Calculation ();  

The document needs to print this code

1
2
3

Although counting can actually begin with and adapt , the behavior of the optimization program will change. This is the reason why you can not escape the calculation of collection anyway. This is one of the reasons for which compiler optimization is very easy in pure functional languages.


Update :. Obviously, it is not clear that it is possible to fully implement and calculate so that select s to < Code> icon selection & lt; T & gt; will still be rated sluggish, but calculation () will be evaluated without computation of collection in O (1). I'm going to do that without changing the interface of any way. T & gt; One such thing is that ICollection is already used :

  Private Interface IDirectlyCountable {Integer calculation {find;}} Private class SelectICollectionIterator & LT ; TSource, TResult & gt; : IEnumerable & lt; T & gt;, indirectly qualified (& lt; TSource, TResult & gt; selector of the ICollection & lt; TSource & gt; Sequence; Public Selector Selector (ECLUTION & lt; TSource & gt; Source, Funk & lt; ; TSource, TResult & gt; selector) {this.sequence = source; this selector = selector;} public int calculation {get {return sequence.Count;}} // ... GetEnumerator ...} public static IEnumerable & Select & lt; TSource, TResult & gt; (this IEnumerable & lt; TSource & gt; source, function & lt; TSource, TResult & gt; selector) {// ... error Defective handling for ... If the new SelectICollectionIterator (source ICollection & lt; TSource & gt;) returns & lt Return; TSource, TrashText> ((Iconicon & lt; TSource & gt; Source, Selector); // ... Rest of the method. ..} Public static int calculation & lt; T & gt; (This IEnumerable & lt; T & gt; source) {// ... ICollection & lt; T & gt; Collection = ICollection as Source & lt; T & gt;; (collection! = Null) return collection; calculation; odd calculating qualified fairness = officially qualified as source; If (countableSequence! = Null) back countableSequence.Count; // ... calculate and count the sequence ...}  

It will still evaluate the calculation the lazy if you change the underlying collection, count Will change and the sequence will not be cached. Only the difference selector will not be able to side effects in the representative.