So let's start with the fact that there are two types of resources - managed and unmanaged. As for the former, there’s no need to worry at all - the garbage collector will take care of them. But with uncontrollable 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 the destructor and the finalizer?
A destructor is a method for deinitializing an object. Here it is important to mention such a thing as deterministic destruction. That is, we know for sure when the object will be deleted. Most often this happens when the object's scope ends, or the programmer explicitly frees the memory (in s / s ++).
But the definition of the finalizer from Wikipedia.
The finalizer is a class method that is automatically called by the runtime between the time when an object of this class is recognized by the garbage collector as unused, and the time the object is deleted (freeing the memory it occupies). This is the opposite thing - nondeterministic destruction.
That is, the main disadvantage 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 thing as just the destructor and finalizer in the ordinary world.
In Visual C #, there is a finalizer that is created using the C ++ creation destructor syntax, and which is even called by some as a destructor, although it is not. It is executed through the Finalize method, which cannot be redefined (in C # it is not, but in VB it is possible), therefore, you have to use the destructor syntax in tilde (~ ClassName). And only when compiled into IL, the compiler calls it Finalize. When executed, this method also calls the finalizer of the parent class.
In general, this question is very, very controversial. Some people think that the destructor and the 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 programming language C # (4.0 at the moment), then there the word "finalizer" never occurs. Well, it can still be explained. The finalizer is closely related to the garbage collector, which in turn is part of the runtime environment (CLR in our case), but not the programming language itself.
Now let's go even further and climb into the CLI specification (ECMA-335). Here is what is written.
Can be defined as an object type.
When an instance of the class is no longer reachable.This is undoubtedly a description of the finalizer, although in my opinion a bit inaccurate.
Next, go to msdn. No article contains the word finalizer in its pure form - but the word destructor is almost always used. A natural question arises - why people call a destructor something that is not. It turns out that maykrosoftovsky developers deliberately changed the meaning of the word. And that's why.
We know that in Visual C # nondeterministic destruction. This means that even if the scope of the object is over, and the garbage collector realized that it is possible to free the memory it occupies, it’s not a fact that it will happen immediately. That is, it is a pure finalizer. Since it uses the syntax that is used in all languages for the destructor, it can be assumed that in Visual C # there is no way to define a destructor (in a general sense). This means that it simply does not exist. Yes, there is no need for it either, but you need to agree that the destructor itself cannot be in Visual C #.
Conclusion - either I am an idiot, and I misunderstood everything completely (and the probability of this is quite high), or I need to come to terms with it, and call the destructor something that looks like it (tilde, hello), but in fact is a finalizer. We must somehow live in this world.