Saturday 22 January 2011

LINQ Quiz...

Can you guess the output of the following code?

var values = new List() { 100, 110, 120 };
var funcs = new List<func<int>>();
foreach (var v in values)
      funcs.Add(() => v);
foreach (var f in funcs)
      Console.WriteLine(f());

If it's not "solved" I will post the answer in the next post...

3 comments:

  1. I will answer with a question :

    Will the following code have the same effect?
    foreach (var v in values)
    {
    int i = v;
    funcs.Add(() => i);
    }

    ReplyDelete
  2. Perfect ;) It will not have the same output indeed!

    ReplyDelete