I executed the following code with C# 3.5 and 4.0. The results are entirely different.
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<IEnumerable<int>> results = new List<IEnumerable<int>>();
foreach (var num in numbers)
{
results.Add(numbers.Where(item => item > num));
}
foreach (var r in results)
{
Console.WriteLine("{0}", r.Count());
}
}
With Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420 the output is 0 0 0 0 0 0 0 0 0 0
.
But with Microsoft (R) Visual C# Compiler version 4.0.30319.17929 the output is 9 8 7 6 5 4 3 2 1 0
.
I have a faint idea that this is because of deferred execution or lazy evaluation but haven't clearly understood how it is responsible for different outputs here.
Correction: Sorry it was .NET 3.5 and 4.5 and also added compiler versions Please explain.