This brain teaser also comes courtesy of Simeon Cran.

What will the following code output? When you think you know, copy the code into Foo.cs and run “csc Foo.cs” and then run “Foo.exe”. Did it output what you expected? The brain teaser is to come up with the correct explanation for why the program outputs what it does.

using System;

struct Foo : IDisposable
{
    bool disposed;
    
    public void Dispose()
    {
        disposed = true;
        Console.WriteLine("Disposed");
    }

    static void Main(string[] args)
    {
        Foo foo;
        using (foo = new Foo())
        {
        }
        Console.WriteLine(foo.disposed);
        using (foo = new Foo())
        {
            foo.Dispose();
        }
        Console.WriteLine(foo.disposed);
    }
}