Why is Square()
being called again in the code below? I suppose values is being recalculated, but why? We got the values
earlier, why is it being called again? Why does the compiler issue a warning "Possible multiple enumeration"?
static IEnumerable<int> Square(IEnumerable<int> a)
{
foreach(var r in a)
{
Console.WriteLine($"From Square {r * r}");
yield return r * r;
}
}
class Wrap
{
private static int init = 0;
public int Value
{
get { return ++init; }
}
}
static void Main(string[] args)
{
var w = new Wrap();
var wraps = new Wrap[3];
for(int i=0; i<wraps.Length; i++)
{
wraps[i] = w;
}
var values = wraps.Select(x => x.Value);
var results = Square(values);
int sum = 0;
int count = 0;
foreach(var r in results)
{
count++;
sum += r;
}
Console.WriteLine("Count {0}", count);
Console.WriteLine("Sum {0}", sum);
Console.WriteLine("Count {0}", results.Count());
Console.WriteLine("Sum {0}", results.Sum());
}
Results:
From Square 1
From Square 4
From Square 9
Count 3
Sum 14
From Square 16
From Square 25
From Square 36
Count 3
From Square 49
From Square 64
From Square 81
Sum 194
Square(values)
returns an IEnumerable
, which is iterated over (called) each time it is accessed.
If you cast the result to a List()
, the results are concreted into a List()
.
var results = Square(values).ToList();
Results with .ToList()
From Square 1
From Square 4
From Square 9
Count 3
Sum 14
Count 3
Sum 14