Dispose() and GC.SuppressFinalize()???

B

Bob

When implementing the Dispose() method, I understand that it's supposed to
do what Finalize() does so that's why it should call GC.SuppressFinalize().
However, it's not clear to me what happens the instance itself, for example
in the following code:

public class Test1 : IDisposable {
public string Hello() {
return "HEllo";
}

public void Dispose() {
System.Diagnostics.Debug.WriteLine("This is disposed");
GC.SuppressFinalize(this);
}
}

I don't see how an instance of this class can be destroyed as there is not
something like "this = null;" in Dispose(), and also Finalize() won't be
called. Does the runtime somehow know that the instance of this class has a
Dispose() method so it should go ahead destroy the instance once the
Dispose() method is called??
 
B

Bob

"goes out of scope" is probably not the right way to describe it. What
happens is once the page processing completes, the Dispose() method would be
called. I'm not sure exactly where it happens but it happens because any
code put in the method would be executed. If the .NET runtime doesn't do it
then it's ASP.NET process doing it. I'm interested in knowing how it's
implemented.
 
J

Jon Skeet [C# MVP]

Bob said:
"goes out of scope" is probably not the right way to describe it. What
happens is once the page processing completes, the Dispose() method would be
called. I'm not sure exactly where it happens but it happens because any
code put in the method would be executed. If the .NET runtime doesn't do it
then it's ASP.NET process doing it. I'm interested in knowing how it's
implemented.

Well, not knowing much about ASP.NET, I would assume that there's
basically some kind of try/finally (effectively) so that whatever
creates the page also then destroys it, just as in the normal kind of
situation of:

using (Something = new Something())
{
....
}

invokes the Dispose method of Something (which must implement
IDisposable).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top