When is destructor called for C# classes in ASP.NE

2019-04-29 04:54发布

问题:

Say, I have my own C# class defined as such:

public class MyClass
{
    public MyClass()
    {
        //Do the work
    }
    ~MyClass()
    {
        //Destructor
    }
}

And then I create an instance of my class from an ASP.NET project, as such:

if(true)
{
    MyClass c = new MyClass();
    //Do some work with 'c'

    //Shouldn't destructor for 'c' be called here?
}

//Continue on

I'd expect the destructor to be called at the end of the if scope but it never is called. What am I missing?

回答1:

The equivalent to a C++ destructor is IDisposable and the Dispose() method, often used in a using block.

See http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

What you are calling a destructor is better known as a Finalizer.

Here's how you would use IDisposable. Note that Dispose() is not automatically called; the best you can do is to use using which will cause Dispose() to be called, even if there is an exception within the using block before it reaches the end.

public class MyClass: IDisposable
{
    public MyClass()
    {
        //Do the work
    }

    public void Dispose()
    {
        // Clean stuff up.
    }
}

Then you could use it like this:

using (MyClass c = new MyClass())
{
    // Do some work with 'C'
    // Even if there is an exception, c.Dispose() will be called before
    // the 'using' block is exited.
}

You can call .Dispose() explicitly yourself if you need to. The only point of using is to automate calling .Dispose() when execution leaves the using block for any reason.

See here for more info: http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.110%29.aspx

Basically, the using block above is equivalent to:

MyClass c = new MyClass();

try
{
    // Do some work with 'C'
}

finally
{
    if (c != null)
        ((IDisposable)c).Dispose();
}


回答2:

There is no way you can control a timing or make a guess on when actually destructor of the object will be called. It's all up to the Garbage Collector.

The only think you can be sure, in this case, that at the moment you leave that scope the object c becomes unreachable (I assume there are no global references to that instance inside the scope), so the instance c is intended to be identified and removed by Garbage Collector when time comes.



回答3:

It is up to the garbage collector as to when an object is freed up. However you can force an object to be freed up by calling Finalize. or GC.Collect which will force the garbage collector to take action.