The code below is perfectly legal.
DisposableObject disposableObject;
...
using (disposableObject = new DisposableObject(...))
{
disposableObject.UseDisposableResource();
}
...
var result = disposableObject.AccessUndisposedResource();
Q Make use of it or stay clear?
There are a couple of norms / expectations involved here:
using
variables are usually declared within the using statement, which exlicitly prevents usage when they have been disposed (again, not compiler-enforced)Your code violates both these norms - you have a disposed object which is still expected to be usable, as well as a using block which disposes an external variable which remains in scope after the block. The result? Confusion and hard-to-read-and-maintain code.
So yes, steer well clear.