C #: How to add a destructor to a custom class?

How do you add a destructor to a class made in C # to destroy it when it is no longer needed?

+3
source share
6 answers

Read this one .

But you might consider introducing IDisposable, which usually offers a more elegant solution. Go here for more information.

+6
source

Do not use a destructor. Use Dispose () and Finalize () instead.

This is a good article on this topic: When and how to use Dispose and Shut Down in C #

+5
source

, . , , Interop/p/invoke.

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

! # .

:


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

:


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

    }
}

, Finalize. .NET Finalize , , Finalize 2 GC, .

++, , , GC .

+1

:

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

MSDN.

0

~ className() {}

0

Source: https://habr.com/ru/post/1702350/


All Articles