Daily .NET quiz

What is the output of the following code:

using System;
using System.Collections.Generic;

public class Program
  static void Main()
    var funcs = new List<Func<string>>();
    var urls = new List<Uri>
      new Uri( "http://google.com" ), 
      new Uri( "http://bing.com" )

    foreach( var u in urls )
      funcs.Add( () => u.ToString() );

    foreach( var f in funcs )
      Console.WriteLine( f() );

A little help from ReSharper:


As the warning says, this piece of code behaves differently if compiled with the C# 4.0 and the C# 5.0 compilers! Actually this is the only breaking change in C# 5.

The problem is not new (check Eric Lippert’s post from 2009), this piece of code is not mine. I borrowed it from Martin Doms’ blog, who started a daily quiz series about .NET, and this is Quiz #006. The series goes for 3 weeks now, and Martin touches really deep topics. Maybe one day you will meet these questions in a job interview, so I definitely recommend reading them 🙂

You can read the full series at http://blog.martindoms.com/tag/quiz/ .

Which question do you like most?


Technorati-címkék: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s