Share via


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.

Peter

Comments

  • Anonymous
    June 03, 2005
    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.
  • Anonymous
    June 06, 2005
    The comment has been removed