Is there a destructor in C #?

    So, to begin with, there are two types of resources - managed and unmanaged. As for the first ones, you don’t have to worry at all - the garbage collector will take care of them. But with unmanaged resources, things are much more complicated. Our garbage man does not know how to free them, so we ourselves have to deal with this issue.

    What is the difference between a destructor and a finalizer

    A destructor is a method for deinitializing an object. It is important to mention such a thing as deterministic destruction. That is, we know exactly when the object will be deleted. Most often this happens when the scope of the object ends, or the programmer explicitly frees memory (in c / s ++).

    And here is the definition of the finalizer from Wikipedia.

    The finalizer is a class method that is automatically called by the runtime in the interval between the moment when the object of this class is recognized by the garbage collector as unused and the moment the object is deleted (freeing the memory it occupies). This is the opposite thing - nondeterministic destruction.
    That is, the main minus of the finalizer is that we do not know when it will be called. This can create a huge amount of problems.

    But the destructor and finalizer in .NET is not the same as just the destructor and finalizer in the ordinary world.

    Visual C # has a finalizer that is created using the syntax for creating a destructor in C ++, and which some even refer to as a destructor, although it is not. It was executed through the Finalize method, which cannot be overridden (in C # it isn’t, but in VB it is possible), and therefore you have to use the destructor syntax via the tilde (~ ClassName). And only when compiling to IL, the compiler calls it Finalize. When executed, this method also calls the finalizer of the parent class.

    In general, this issue is very, very controversial. Some people think that the destructor and finalizer in .NET mean exactly the same thing and differ only in name, others think that there is a huge difference, and confusing them is a crime.

    If you look at the specification of the C # programming language (4.0 at the moment), then the word "finalizer" never appears there. Well, this can still be explained. The finalizer is closely related to the garbage collector, which in turn is part of the runtime (CLR in our case), but not the programming language itself.

    Now let's go even further and get into the CLI specification (ECMA-335). Here is what is written.

    A class definition that creates an object type can supply an instance method (called a finalizer) to be called
    when an instance of the class is no longer reachable.


    This is undoubtedly a description of the finalizer, although in my opinion, a little inaccurate.

    Next, go to msdn. In no article does the word finalizer appear in its purest form - but the word destructor is almost always used. A logical question arises - why do people call the destructor what they are not. It turns out that Microsoft developers have deliberately changed the meaning of this word. And that's why.

    We know that in Visual C # nondeterministic destruction. This means that even if the scope of the object has ended, and the garbage collector realized that it is possible to free the memory it occupies, it is not a fact that this will happen immediately. That is, this is pure finalizer. Since it uses the syntax that is used for a destructor in all languages, it can be assumed that in Visual C # there is no way to define a destructor (in general terms). This means that it is simply not there. Yes, there is no special need for it either, but you have to agree that there can be no destructor in Visual C # either.

    The conclusion is either I'm an idiot and I completely misunderstood everything (and the probability of this is quite high), or you need to come to terms with it and call the destructor something that looks like it (tilde, hello), but in fact it is a finalizer. We must somehow live in this world.

    Also popular now: