[Please do not mail me a copy of your followup]
Jon Skeet [C# MVP] <
[email protected]> spake the secret code
That's just an example off the top of my head, and it's not terribly
good. I'm sure it's quite rare, but there are probably a few real cases
around
There are lots of examples where anonymous items in a using statement
are useful. Basically, any time you have to do something like this:
try
{
StartSomething();
// do stuff
}
finally
{
EndSomething();
}
You can encapsulate the Start/End inside an IDisposable class. An
extremely simple example is a helper class that puts up a wait cursor
around a potentially long operation:
public class CursorWaiter : IDisposable
{
public CursorWaiter()
{
Cursor.Current = Cursors.WaitCursor;
}
~CursorWaiter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
Cursor.Current = Cursors.Default;
}
}
}
// ...
using (new CursorWaiter())
{
// do slow stuff
}
You don't need a name for the CursorWaiter() instance because you
don't need to access it within the enclosed scope.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<
http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<
http://pilgrimage.scene.org>