I work in C#, and I\'ve been pretty lax about using using
blocks to declare objects that implement IDisposable
, which you\'re apparently always suppose
This is why (IMHO) C++'s RAII is superior to .NET's using
statement.
A lot of people said that IDisposable
is only for un-managed resources, this is only true depending on how you define "resource". You can have a Read/Write lock implementing IDisposable
and then the "resource" is the conceptual access to the code block. You can have an object that changes the cursor to hour-glass in the constructor and back to the previously saved value in IDispose
and then the "resource" is the changed cursor. I would say that you use IDisposable when you want deterministic action to take place when leaving the scope no matter how the scope is left, but I have to admit that it's far less catchy than saying "it's for managing un-managed resource management".
See also the question about why there's no RAII in .NET.