I have noticed strange Linq select
behavior in program below:
IEnumerable<string> s = new List<string>() { "1", "2", "3" };
var i = s.Select(url =>
{
Console.WriteLine(url);
url = string.Format("--{0}--",url);
return url;
}
);
Console.WriteLine("done with selector");
foreach (string f in i)
{
Console.WriteLine("f is {0}", f);
}
Output is:
1
f is --1--
2
f is --2--
3
f is --3--
I was expecting output to be :
1
2
3
f is --1--
f is --2--
f is --3--
How to explain such strange behavior? Is it some kind of code optimization?