析构函数何时在ASP.NET中调用C#类?

时间:2022-10-06 20:36:55

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

说,我有自己的C#类定义如下:

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:

然后我从ASP.NET项目创建我的类的实例,如下所示:

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?

我希望在if范围的末尾调用析构函数,但它永远不会被调用。我错过了什么?

3 个解决方案

#1


6  

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

等效于C ++析构函数的是IDisposable和Dispose()方法,通常在using块中使用。

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.

以下是使用IDisposable的方法。请注意,不会自动调用Dispose();你可以做的最好的事情是使用use会导致调用Dispose(),即使在使用块到达结尾之前有异常。

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.

如果需要,您可以自己显式调用.Dispose()。唯一的用途是当执行因任何原因离开using块时自动调用.Dispose()。

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

有关详细信息,请参阅此处: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


4  

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.

在这种情况下,唯一的想法是,您可以确定,当您离开该范围时,对象c变得无法访问(我假设在范围内没有对该实例的全局引用),因此实例c旨在被识别并在时间到来时被垃圾收集器删除。

#3


2  

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.

关于何时释放对象,由垃圾收集器决定。但是,您可以通过调用Finalize来强制释放对象。或GC.Collect将强制垃圾收集器采取行动。

#1


6  

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

等效于C ++析构函数的是IDisposable和Dispose()方法,通常在using块中使用。

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.

以下是使用IDisposable的方法。请注意,不会自动调用Dispose();你可以做的最好的事情是使用use会导致调用Dispose(),即使在使用块到达结尾之前有异常。

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.

如果需要,您可以自己显式调用.Dispose()。唯一的用途是当执行因任何原因离开using块时自动调用.Dispose()。

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

有关详细信息,请参阅此处: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


4  

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.

在这种情况下,唯一的想法是,您可以确定,当您离开该范围时,对象c变得无法访问(我假设在范围内没有对该实例的全局引用),因此实例c旨在被识别并在时间到来时被垃圾收集器删除。

#3


2  

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.

关于何时释放对象,由垃圾收集器决定。但是,您可以通过调用Finalize来强制释放对象。或GC.Collect将强制垃圾收集器采取行动。