+2  A: 

You should not be using a using block in this case, since you need the object after the block has finished. It is only to be used when there is a clear starting and ending point of the lifetime of the object.

John Saunders
+11  A: 

Disposing an object does not destroy it; it simply tells it to clean up any unmanaged resources it uses as they are no longer needed. In your example, you're creating a disposable object, assigning to a dictionary, and then just telling it to remove some resources.

The correct scenario for the using statement is when you want to initialize a resource, do something with it, and then destroy it and forget about it; for example:

using (var stream = new FileStream("some-file.txt"))
using (var reader = new StreamReader(stream))
{
    Console.Write(reader.ReadToEnd());
}

If you want to retain the object after you've used it, you shouldn't be disposing it, and hence a using statement should not be used.

Will Vousden
Fair enough. I was trying to get an understanding of what is happening. If the object is suppose to be disposed, or the resources associated with that object. +1
dboarman
+1  A: 

The IDisposable interface indicates that a type manages some kind of resource. The Dispose method exists to allow you to dispose of the resources used by an instance without having to wait for garbage-collection to take place and the resources to be freed by a finalizer.

In your example, the dictionary is still containing a reference to the disposable class, but the instance will have been disposed at the end of the using block. Subsequent attempts to call methods on the instance will now likely throw ObjectDisposedException or InvalidOperationException, to indicate the instance is no longer in a "working" state.

Disposing an IDisposable is not to be confused with releasing the memory occupied the instance, or invoking any garbage-collection routines on it. The instance is tracked and managed by the garbage-collector like any other, only to be released when the garbage-collector decides it.

Programming Hero
@Programming Hero: the 2nd paragraph is partially correct...it depends on proper disposal of the resources and if any class members access those disposed resources. It still seems like a weird behavior. If you edit that, I'll remove the down-vote. ;)
dboarman
The behaviour of a disposed instance is largely undefined; the expectation is to assume that an instance will no longer behave normally once disposed, as some resource it depends on will have been released. It is likely that some properties will still return values normally, but most methods should be expected to fail.
Programming Hero
+2  A: 

It's important to remember that IDisposable, while a slightly special interface, is an interface nonetheless. When the using block exits, it calls Dispose() on your object. Nothing more. Your reference is still valid and, if your Dispose method does nothing, your object will be completely unaffected. If you don't keep track the disposal and explicitly throw exceptions, then you won't get any exceptions after that point because there is no inherent disposed state in .NET.

Nick
+1 @Nick: good point about `IDispose'.
dboarman