Created
July 5, 2011 19:57
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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