C# Twisters–Round 2

More brain twisters. From http://stackoverflow.com/questions/241134/what-is-the-worst-gotcha-in-c-sharp-or-net, http://www.softwire.com/blog/2012/08/13/dont-be-too-lazy-linqs-lazy-evaluation-gotchas/, and others.

11.

static void CSharpPuzzle11()
{
    // Which implementation is faster?
    // Which implementation uses more memory?
    { // Implementation A.
        var before = DateTime.Now;
        int size = 100000;
        string a = string.Empty;
        for (int i = 0; i < size; ++i) a += i.ToString() + " ";
        var after = DateTime.Now;
        Console.WriteLine(a.SizeOf());
        Console.WriteLine(after - before);
    }
    { // Implementation B.
        var before = DateTime.Now;
        int size = 100000;
        StringBuilder a = new StringBuilder();
        for (int i = 0; i < size; ++i) a.Append(i.ToString() + " ");
        var after = DateTime.Now;
        Console.WriteLine(a.SizeOf());
        Console.WriteLine(after - before);
    }
}

12.

class Beer
{
    public bool IsAvailable;
    public bool IsNice;
    public string Name;
}

static void CSharpPuzzle12()
{
    Func<Beer, bool> BeerIsNice = (Beer b) =>
    {
        Console.WriteLine(b.Name);
        return b.IsNice;
    };

    List<Beer> availableBeers = new List<Beer>()
    {
        new Beer { Name = "Old Tom's", IsNice = false, IsAvailable = true },
        new Beer { Name = "Young Dan's", IsNice = true, IsAvailable = false },
        new Beer { Name = "Now Beer", IsNice = false, IsAvailable = true }
    };
    int count = availableBeers.Where(BeerIsNice).Count(); // What is the value of count?
    Console.WriteLine(availableBeers.Where(BeerIsNice).First().Name); // What is the output?
}


13.

private static IEnumerable<char> Keystrokes()
{
    while (true)
    {
        yield return Console.ReadKey(true).KeyChar;
    }
}

static void CSharpPuzzle13()
{
    // Why is keyboard read twice four times?
    System.Console.WriteLine("Enter 'beer'.");
    var firstFourKeystrokes = Keystrokes().Take(4);
    if (firstFourKeystrokes.SequenceEqual(new[] {'b', 'e', 'e', 'r'}))
    {
        System.Console.WriteLine("You entered 'beer'. Getting each char now...");
        firstFourKeystrokes.ToList().ForEach(Console.Write);
    }
}

14.

static void CSharpPuzzle14()
{
    // What's the correct way to create a new GUID?
    var which_one1 = new Guid().ToString();
    var which_one2 = Guid.NewGuid();
}

15.

static void CSharpPuzzle15()
{
    // What happens if you run this code?
    var t = new Program();
    var x = t.MyVar;
}

private int myVar;
public int MyVar
{
    get { return MyVar; }
}

16.

static void CSharpPuzzle16()
{
    // Why don't these work? How do we fix the problem?
    var a = Type.GetType("string");
    var b = Type.GetType("Program");
    var c = Type.GetType("System.Data.Entity.DbContext");
}

17.

static void CSharpPuzzle17()
{
    // Why doesn't this work? How do we fix it?
    for (int i = 0; i < 10; i++)
    {
        ThreadStart ts = delegate { Console.WriteLine(i); };
        new Thread(ts).Start();
    }
}

18.

static IEnumerable<char> CapitalLetters(string input)
{
    if (input == null)
    {
        throw new ArgumentNullException(input);
    }
    foreach (char c in input)
    {
        yield return char.ToUpper(c);
    }
}

static void CSharpPuzzle18()
{
    // Why doesn't this code throw an exception?
    try
    {
        var x = CapitalLetters(null);
        Console.WriteLine("An exception should have been thrown!");
    }
    catch (ArgumentNullException)
    {
        // Expected
    }
}

19.

static void CSharpPuzzle19()
{
    DateTime a = DateTime.Now;
    Thread.Sleep(2000);
    DateTime b = DateTime.Now;
    TimeSpan span = a - b;

    // Which is correct, the first or second line below?
    Console.WriteLine(span.Seconds);
    Console.WriteLine(span.TotalSeconds);
}

20.

static void CSharpPuzzle20()
{
    string my = "my ";
    var t1 = my + "string" == "my string"; //true

    var a = new ArrayList();
    a.Add(my + "string");
    a.Add("my string");

    // uses ==(object) instead of ==(string)
    var t2 = a[1] == "my string"; // True or False?
    var t3 = a[0] == "my string"; // True or False?

    var t4 = my + "string" == "my string"; // True or False?
}

21.

static void CSharpPuzzle21()
{
    // What does this output?
    string prefix1 = "C:\\MyFolder\\MySubFolder";
    string prefix2 = "C:\\MyFolder\\MySubFolder\\";
    string suffix1 = "log\\";
    string suffix2 = "\\log\\";

    Console.WriteLine(Path.Combine(prefix1, suffix1));
    Console.WriteLine(Path.Combine(prefix1, suffix2));
    Console.WriteLine(Path.Combine(prefix2, suffix1));
    Console.WriteLine(Path.Combine(prefix2, suffix2));
}