CHAPTER 13 UNDERSTANDING OBJECT LIFETIME
If you looked at the following CIL code of the Main() method using ildasm.exe, you would find the
using syntax does indeed expand to try/finally logic, with the expected call to Dispose():
.method private hidebysig static void Main(string[] args) cil managed
{
...
.try
{
...
} // end .try
finally
{
...
IL_0012: callvirt instance void
SimpleFinalize.MyResourceWrapper::Dispose()
} // end handler
...
} // end of method Program::Main
Note If you attempt to “use” an object that does not implement IDisposable, you will receive a compiler error.
While this syntax does remove the need to manually wrap disposable objects within try/finally
logic, the C# using keyword unfortunately now has a double meaning (importing namespaces and
invoking a Dispose() method). Nevertheless, when you are working with .NET types that support the
IDisposable interface, this syntactical construct will ensure that the object “being used” will
automatically have its Dispose() method called once the using block has exited.
Also, be aware that it is possible to declare multiple objects of the same type within a using scope. As
you would expect, the compiler will inject code to call Dispose() on each declared object.
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Dispose *****\n");
}
// Use a comma-delimited list to declare multiple objects to dispose.
using(MyResourceWrapper rw = new MyResourceWrapper(),
rw2 = new MyResourceWrapper())
{
// Use rw and rw2 objects.
}
Source Code The SimpleDispose project is included under the Chapter 13 subdirectory.
492