A Quick note on using statements in C#

Nicholas Paldino(caspershouse.com) noticed a change in the C# using statement between Beta 1 and Beta 2 of VS 2005. Here's the full story ...

In V1 and V1.1, the C# compiler accepted only types implementing IDisposable in using statements. In Beta1 of Whidbey we added support for the pattern based resource types, and in Beta2 we reverted to the V1 behavior.

A pattern based resources is any type which contains a public Dispose method. A pattern based resource type need not implement the IDisposable interface. The motivation for pattern based resource types was that in V1 CLR there was no way to call IDisposable.Dispose on a value type without boxing. In V1 the C# compiler attempted to optimize certain cases, but that optimization was not versioning resilient.

In Beta 2, we changed the IL generated for the Dispose call to use the new .constrain prefix (added in V2 for generics) which enables making the call to Dispose without boxing. Also, allowing pattern based resource types could result in change in behavior of valid V1 code in some, admittedly contrived, scenarios.

This removed the motivation for pattern based resource types, so we removed support for them in Beta2.


Comments (2)

  1. Garry Trinder says:

    Why you has reverted this ?

    Can this be a compiler switch to make it strict as V1 if somebody wish so, but keep Beta1 by default for all others ?

    I think it’s all on compiler level – no needs for runtime support.

  2. MSDN Archive says:

    This change will break some code written using Beta1, which is unfortunate, exploring some design alternatives is what Beta releases is all about.

    Adding a compiler option to switch the behavior of the using statement would get us on a very slippery slope. Pretty soon, there’d be compiler options controlling all kinds of language semantics. This would add a ton of complexity to programmers reading C# code as they would have to know which compiler flags the code was compiled under to understand the code.

    Currently the only compiler option which controls language sematics is the /langversion option allowing V1 or V2 behavior.

Skip to main content