Finalizers and destructors, what's Wikipedia saying?
Asked Answered
P

6

1

As I understand there are two camps concerning this question - the first one thinks that finalizer is a destructor specific to C#. So they think that these two things are the same.

The second camp thinks that there's a slight difference - written in Wikipedia - "the term “destructor” is typically used to mean a deterministically-invoked cleanup, whereas a “finalizer” runs when the garbage collector says to run it."

But let me clarify something for myself. Deterministically-invoked cleanup? In C# specification and msdn it's written that destructors cannot be invoked(they are invoked automatically). The only case when they may be invoked automatically is by garbage collector.

So I don't see any difference between deterministically-invoked cleanup and the case with the garbage collector.

Is it so or not?

Pino answered 4/5, 2011 at 11:37 Comment(0)
V
1

In C#, finalizer and destructor are different names for the same thing.

The C# language specification (1.6.7.6) actually refers to these as destructors. However, as the name destructor may easily be mistaken for the C++ counterpart (which is quite different from a destructor in C#) it makes sense to use the term finalizer instead.

Vullo answered 4/5, 2011 at 11:40 Comment(5)
that's why I'm so confused about this topic blogs.msdn.com/b/ericlippert/archive/2010/01/21/…Pino
@Sergey: What do you find confusing about that? Eric Lippert states that the language specification uses the incorrect term according to the definition on Wikipedia.Vullo
So, who should we believe - language spec or Wikipedia?Pino
@Sergey: They can both be correct. Wikipedia gives the general definition for the words. The C# spec details the definition of the language. In that context the definition stands even if it doesn't match the terminology defined by Wikipedia. So in C# a destructor is a finalizer according to Wikipedia. As Eric Lippert pointed out, the spec got the naming wrong.Vullo
They're not quite the same thing. A destructor for a class is a block of code which will request that the compiler generate an override of Object.Finalize() (i.e. a finalizer) containing the indicated code plus a call to Base.Finalize(). I'll confess to some puzzlement as to why C# goes out of its way not to allow one to simply override Finalize() or call Base.Finalize().Majunga
N
5

There is a huge difference. Deterministically invoked means you know, when it it will be invoked.

In C++:

{
    Person a;
    a.Name = "John";
    InvitePerson(a);
} // <-- Destructor is always invoked when the scope is left

In C#:

{
    Person a = new Person();
    a.Name = "John";
    InvitePerson(a);
} // <-- The finalizer is NOT invoked when the scope is left

As pointed out, the main difference is:
In C# and other garbage collected languages, you don't know when or even if the finalizer will be executed.
In C++ however, you do know when and that the destructor is executed as soon as the object goes out of scope.

Neurotic answered 4/5, 2011 at 11:42 Comment(4)
and what about destructor in C#?Pino
The thing that comes closest is IDisposable.Dispose.Neurotic
So, there are no destructors in C#?Pino
The thing that comes closest is IDisposable.Dispose. At least if you mean destructors in the terms of wikipedia. As others have noted, even in the C# spec are the finalizers called destructors... To answer your question: No, there are not desctructors in the terms of wikipedia or C++. You need to emulate it with IDisposable.Dispose in conjunction with the using() block.Neurotic
S
3

The official stance is that C# has destructors (~ClassName() {} ) and they map onto System.Object.Finalize(). In VB.NET you just override Finalize much like ToString.

There is some confusion though, you're right about that. But it's more about Finalize/Destructor vs Dispose. Here is a post from Eric Lippert (referring to Wikipedia):

Yes, by these definitions, the C# spec gets it wrong. What we call a “destructor” in the spec is actually a finalizer, and what we call the “Dispose()” method invoked by a “using” statement is in fact a “destructor”.

Salubrious answered 4/5, 2011 at 11:42 Comment(0)
V
1

In C#, finalizer and destructor are different names for the same thing.

The C# language specification (1.6.7.6) actually refers to these as destructors. However, as the name destructor may easily be mistaken for the C++ counterpart (which is quite different from a destructor in C#) it makes sense to use the term finalizer instead.

Vullo answered 4/5, 2011 at 11:40 Comment(5)
that's why I'm so confused about this topic blogs.msdn.com/b/ericlippert/archive/2010/01/21/…Pino
@Sergey: What do you find confusing about that? Eric Lippert states that the language specification uses the incorrect term according to the definition on Wikipedia.Vullo
So, who should we believe - language spec or Wikipedia?Pino
@Sergey: They can both be correct. Wikipedia gives the general definition for the words. The C# spec details the definition of the language. In that context the definition stands even if it doesn't match the terminology defined by Wikipedia. So in C# a destructor is a finalizer according to Wikipedia. As Eric Lippert pointed out, the spec got the naming wrong.Vullo
They're not quite the same thing. A destructor for a class is a block of code which will request that the compiler generate an override of Object.Finalize() (i.e. a finalizer) containing the indicated code plus a call to Base.Finalize(). I'll confess to some puzzlement as to why C# goes out of its way not to allow one to simply override Finalize() or call Base.Finalize().Majunga
J
1

The second opinion is more correct. You can't ensure that the destructor of an object will be run at a specified time. So it is not a destructor, like in C++, where you can explicit call delete obj.

In .NET, for objects that need to clean up some resources after usage, you should implement IDisposable and call Dispose explicitly when you are finished with the object, or with a using() block.

Jacquline answered 4/5, 2011 at 11:41 Comment(0)
F
0

Deterministically means that you can release the memory at any given time. This is not possible in C# since the destructor is invoked by the GC through the finalizer thread at an indefinite point in time.

Furan answered 4/5, 2011 at 11:42 Comment(0)
R
0

I think this is just a case of differing terminology. Words are defined/used differently in different texts - happens all the time.

However there is no "deterministically invoked cleanup" if you are relying on the garbage collector. Your conclusion:

So I don't see any difference between deterministically-invoked cleanup and the case with the garbage collector.

... doesn't make sense in the context you give it. The difference is that one is deterministically invoked, and the other is not.

Ruder answered 4/5, 2011 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.