Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd class member syntax shown in ILSpy

I'm poking around a few dlls within the XNA framework using ILSpy and came across this:

class KerningHelper
{
    private void !KerningHelper()
    {
        ((IDisposable)this).Dispose();
    }
}

What is the exclamation mark for in the above? Is it an issue with ILSpy, or something else?

Note, the class has a separate destructor: private unsafe void ~KerningHelper().

like image 228
George Duckett Avatar asked Apr 17 '12 08:04

George Duckett


1 Answers

As the comments stated, the exclamation mark is the C++/CLI marker for a finalizer method. Unlike traditional C++ destructors (~) that are called when you explicitly dispose of an object, finalizers are called by the garbage collector thread. You can see the official details here.

I would expect ILSpy to translate the !KerningHelper() to ~KerningHelper(), since C++/CLI finalizer is equivalent to C#'s destructor - it's a non-deterministic method that occurs when the GC gets to it, unlike C++/CLI's explicit ~destructor, which is called when you call delete or an explicit Dispose call is made.

like image 120
Avner Shahar-Kashtan Avatar answered Nov 01 '22 16:11

Avner Shahar-Kashtan