Skip to content

Instantly share code, notes, and snippets.

@chaliy
Created July 5, 2011 19:57
Show Gist options
  • Save chaliy/1065756 to your computer and use it in GitHub Desktop.
Save chaliy/1065756 to your computer and use it in GitHub Desktop.
Shows possible problem with IEnumerable (http://blog.chaliy.name/post/7274777462/fooken-by-ienumerable).
public static void Main (string[] args)
{
var items = from i in new []{1}
let x = SideEffect(i)
select i;
Somemething(items);
}
public static string SideEffect(int i){
Console.WriteLine("Side effect");
return i.ToString();
}
public static void Somemething(IEnumerable<int> input){
var c = input.Count(); // This will evaluate first time
foreach(var i in input){ // This will evaluate second time
var ii = i;
}
var d = input.Any(); // This will evaluate third time
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment