Brain Teaser #3 Answer

If you haven't read the question yet, you can read it here.

The answer for this lies in the C# specification. Specifically, read section 8.13. There are two different ways you can use a using statement and in this case we are interested in the second way. The key words from the spec are "in this case ResourceType is implicitly the compile-time type of the expression, and the resource variable is inaccessible in, and invisible to, the embedded statement."

So effectively what the code in our question means is:

 Foo x;
using (/*y =*/ x = new Foo())
{
} // this calls y.Dispose();

In this example, y is effectively the copy of x that is made so that x becomes inaccessible and invisible to the embedded statement. If Foo is a reference type, you don't really see the difference because x and y point to the same type. However, if Foo is a value type, it is actually making a copy of the value and you're dealing with a completely different memory location.