I made a simple class for a small project and decided to just add a destructor for quick impl instead of using it IDisposable, and I came across a compiler error whenever it has a destructor with an access modifier.
public class MyClass
{
public ~MyClass()
{
}
}
I tried public, private, secure and internal. It worked perfectly without access modifiers. Since this article shows that ~ the destructor is essentially syntactic sugar for the protected Finalize function, it seems strange to me that you cannot use at least the protected on the destructor. The article says: "Destructors cannot be called, they are called automatically." Is this behavior true?
In the end, I just used IDisposable, but I'm curious ... is there another reason why you can't put access modifiers in a destructor ?
source
share