tags:

views:

37

answers:

1

Hi

Just wanna to know objSample.dispose() is equal to objSample = null

Indeed, can I get answer to such these questions from disassemblers? how?

Thank you

+1  A: 

No, Disposing an object does not mean setting the reference to that object to null.

Disposing is an convention to clean up resources when the programmer wants it, not to wait untill the garbage collector decides to kick in.

To answer your question, write this:

objSample.Dispose();
objSample = null;
Stormenet
Note that setting `objSample = null` does nothing other than clear the value of a variable; it does not do anything to the actual instance. **Note also that `Dispose()` is not simply an alternative to garbage collection. The `IDisposable` interface is 100% unrelated to garbage collection**.
Adam Robinson
@Adam: So, by objSample=null, we clear the value of a variable which is pointing to an object. Then we can call that object as an "Orphan" and so it's a good choice for G.C. to "collect" it. Is it right? And what does happen when we call dispose method ?
odiseh
@odiseh: Assuming that it is the one and only reference to that object, then yes, it would then be eligible for garbage collection. Note that it's not guaranteed to be collected *ever*, it just means that it's *eligible*. As for what happens when you call `Dispose`, that depends on the class. `Dispose` is not a special method, it's just a member of the `IDisposable` interface. To be precise, what happens when you call `Dispose` is whatever code is in the `Dispose` method for that class.
Adam Robinson
@odiseh: the `Dispose` method simply gets executed. What exactly it does depends on the class of the object, because it's up to the class to implement that method. _Conventionally_, it immediately released all resources the object might be holding (e.g. unmanaged memory, opened file handles etc), except for the memory taken up by the object instance itself - an object has no control over that, so GC has to take care of that later on.
Pavel Minaev
@Pavel: ok, you say "it immediately released all resources the object might be holding......except for the memory taken up by the object instance itself - an object has no control over that, so GC has to take care of that later on". When we call dispose method of that object, what happens to the variable has pointed to it till now?
odiseh
Stormenet
@odiseh: You're putting too much thought behind the `Dispose` method. Think of it like this: you have a class that needs to do some clean up work once the user is done with it, so you put a method on it called `CleanUp` and tell the user that they should call it when they're done. Obviously there is nothing *particularly* special about this method, it's just another function on your class. `Dispose` is the same way. Nothing happens to the variable itself.
Adam Robinson