C#:如何在自定义类中添加析构函数?

时间:2020-12-26 20:35:31

How do you add a destructor to a custom made class made in C# so as to destroy it when it's not needed anymore?

如何在C#中定制的类中添加析构函数,以便在不再需要时将其销毁?

6 个解决方案

#1


Read this.

But you might consider implementing IDisposable which usually offers a more elegant solution. Go here for more info.

但您可以考虑实施IDisposable,它通常提供更优雅的解决方案。到这里获取更多信息。

#2


Don't use a destructor. Use Dispose() and Finalize() instead.

不要使用析构函数。请改用Dispose()和Finalize()。

This is a quite good article on this subject: When and How to Use Dispose and Finalize in C#

这是关于这个主题的一篇非常好的文章:何时以及如何在C#中使用Dispose和Finalize

#3


Destructors are not used to destroy objects as such, there are used to clean up used resources when your object is destroyed. Destructors are usually used to clean up any unmanaged resources such as when using Interop / p/invoke.

析构函数不用于销毁对象,用于在销毁对象时清理已使用的资源。析构函数通常用于清理任何非托管资源,例如使用Interop / p / invoke时。

class Person
{
    // Destructor
    ~Person()
    {
        // Cleanup resources that the object used here.
    }
}

#4


Be really careful!!! You should try and avoid the use of destructors in C# as much as possible.

要小心!!!你应该尽量避免在C#中使用析构函数。

When your code is:

当你的代码是:


class Person
{
    // Destructor
    ~Person()
    {
        // Clean-up resources that the object used here.
    }
}

the compiler is generating this code for you:

编译器为您生成此代码:


class Person
{
    // Destructor
    public override void Finalize()
    {
        // your clean up
        base.Finalize();

    }
}

As you can see you are just implementing the Finalize method. In the .Net platform a bad implementation of the Finalize method can make your application to have a memory leak and if correctly implemented, and object with a Finalize method implementation takes 2 GC calls to be released.

如您所见,您只是实现了Finalize方法。在.Net平台中,Finalize方法的错误实现会使您的应用程序出现内存泄漏,如果正确实现,则使用Finalize方法实现的对象需要释放2个GC调用。

If you are coming from C++ you should just forget about the idea of a destructor at all, you do not need them the GC should take care of you normal classes.

如果你来自C ++,你应该忘记析构函数的想法,你不需要它们GC应该照顾你的普通类。

#5


Here's an example:

这是一个例子:

class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}

More info on MSDN.

有关MSDN的更多信息。

#6


~className() {}

#1


Read this.

But you might consider implementing IDisposable which usually offers a more elegant solution. Go here for more info.

但您可以考虑实施IDisposable,它通常提供更优雅的解决方案。到这里获取更多信息。

#2


Don't use a destructor. Use Dispose() and Finalize() instead.

不要使用析构函数。请改用Dispose()和Finalize()。

This is a quite good article on this subject: When and How to Use Dispose and Finalize in C#

这是关于这个主题的一篇非常好的文章:何时以及如何在C#中使用Dispose和Finalize

#3


Destructors are not used to destroy objects as such, there are used to clean up used resources when your object is destroyed. Destructors are usually used to clean up any unmanaged resources such as when using Interop / p/invoke.

析构函数不用于销毁对象,用于在销毁对象时清理已使用的资源。析构函数通常用于清理任何非托管资源,例如使用Interop / p / invoke时。

class Person
{
    // Destructor
    ~Person()
    {
        // Cleanup resources that the object used here.
    }
}

#4


Be really careful!!! You should try and avoid the use of destructors in C# as much as possible.

要小心!!!你应该尽量避免在C#中使用析构函数。

When your code is:

当你的代码是:


class Person
{
    // Destructor
    ~Person()
    {
        // Clean-up resources that the object used here.
    }
}

the compiler is generating this code for you:

编译器为您生成此代码:


class Person
{
    // Destructor
    public override void Finalize()
    {
        // your clean up
        base.Finalize();

    }
}

As you can see you are just implementing the Finalize method. In the .Net platform a bad implementation of the Finalize method can make your application to have a memory leak and if correctly implemented, and object with a Finalize method implementation takes 2 GC calls to be released.

如您所见,您只是实现了Finalize方法。在.Net平台中,Finalize方法的错误实现会使您的应用程序出现内存泄漏,如果正确实现,则使用Finalize方法实现的对象需要释放2个GC调用。

If you are coming from C++ you should just forget about the idea of a destructor at all, you do not need them the GC should take care of you normal classes.

如果你来自C ++,你应该忘记析构函数的想法,你不需要它们GC应该照顾你的普通类。

#5


Here's an example:

这是一个例子:

class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}

More info on MSDN.

有关MSDN的更多信息。

#6


~className() {}