Alois Kraus

blog

  Home  |   Contact  |   Syndication    |   Login
  108 Posts | 8 Stories | 295 Comments | 162 Trackbacks

News



Article Categories

Archives

Post Categories

Image Galleries

Programming

March 2011 Entries

What does this code print and why? HashSet<int> set = new HashSet<int>(); int[] data = new int[] { 1, 2, 1, 2 }; var unique = from i in data where set.Add(i) select i; // Compiles to: var unique = Enumerable.Where(data, (i) => set.Add(i)); foreach (var i in unique) { Console.WriteLine("First: {0}", i); } foreach (var i in unique) { Console.WriteLine("Second: {0}", i); } The output is: First: 1 First: 2 Why is there no output of the second loop? The reason is that LINQ does not cache ......