Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:05:05+00:00 2026-06-09T23:05:05+00:00

I was browsing the decompiled source code for a DLL in Reflector, and I

  • 0

I was browsing the decompiled source code for a DLL in Reflector, and I came across this C# code:

protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1)
{
    if (flag1)
    {
        this.~ClassName();
    }
    else
    {
        base.Finalize();
    }
}

My first reaction was “What? I thought you couldn’t call the finalizer manually!”

Note: The base type is object.

To be sure it wasn’t a Reflector quirk, I opened up the method in ILSpy. It generated similar code.

I went to Google to confirm my new discovery. I found the documentation for Object.Finalize, and this is what it said:

Every implementation of Finalize in a derived type must call its base type’s implementation of Finalize. This is the only case in which application code is allowed to call Finalize.

Now I don’t what to think. It might be because the DLL was compiled with C++. (Note: I couldn’t find the implementation of Dispose. Maybe it’s auto-generated.) It might be a special allowance for the IDisposable.Dispose method. It might be a flaw in both decompilers.

Some observations:

  • I couldn’t find the implementation of Dispose in the source code. Maybe it’s auto-generated.
  • Reflector shows a method named ~ClassName. It seems as though this method might not actually be the finalizer, but the C++ destructor, or even an ordinary method.

Is this legal C#? If so, what is different about this case? If not, what is actually happening? Is it allowed in C++/CLI, but not C#? Or is it just a glitch in the decompiler?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:05:06+00:00Added an answer on June 9, 2026 at 11:05 pm

    As the other answerers have noted, you’re correct, the reason the disposal code is different is because it’s C++/CLI.

    C++/CLI uses a different idiom for writing cleanup code.

    • C#: Dispose() and ~ClassName() (the finalizer) both call Dispose(bool).
      • All three methods are written by the developer.
    • C++/CLI: Dispose() and Finalize() both call Dispose(bool), which will call either ~ClassName() or !ClassName() (destructor & finalizer, respectively).
      • ~ClassName() and !ClassName() are written by the developer.
        • As you noted, ~ClassName() is treated differently than in C#. In C++/CLI, it stays as a method named “~ClassName”, whereas ~ClassName() in C# gets compiled as protected override void Finalize().
      • Dispose(), Finalize(), and Dispose(bool) are written solely by the compiler. As it does so, the compiler does things that you’re not normally supposed to.

    To demonstrate, here’s a simple C++/CLI class:

    public ref class TestClass
    {
        ~TestClass() { Debug::WriteLine("Disposed"); }
        !TestClass() { Debug::WriteLine("Finalized"); }
    };
    

    Here’s the output from Reflector, decompiling to C# syntax:

    public class TestClass : IDisposable
    {
        private void !TestClass() { Debug.WriteLine("Finalized"); }
        private void ~TestClass() { Debug.WriteLine("Disposed"); }
    
        public sealed override void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        [HandleProcessCorruptedStateExceptions]
        protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool disposing)
        {
            if (disposing)
            {
                this.~TestClass();
            }
            else
            {
                try
                {
                    this.!TestClass();
                }
                finally
                {
                    base.Finalize();
                }
            }
        }
    
        protected override void Finalize()
        {
            this.Dispose(false);
        }
    }
    

    Edit

    It looks like C++/CLI handles constructor exceptions better than C#.

    I wrote test apps in both C++/CLI and C#, which define a Parent class and a Child class, where the Child class’s constructor throws an exception. Both classes have debug output from their constructor, dispose method, and finalizer.

    In C++/CLI, the compiler wraps the contents of the child constructor in a try/fault block, and calls the parent’s Dispose method in fault. (I believe the fault code is executed when the exception is caught by some other try/catch block, as opposed to a catch or finally block where it would be executed immediately, before moving up the stack. But I may be missing a subtlety there.) In C#, there’s no implicit catch or fault block, so Parent.Dispose() is never called. Both languages will call both the child & parent finalizers, when the GC gets around to collecting the objects.

    Here’s a test app I compiled in C++/CLI:

    public ref class Parent
    {
    public:
        Parent() { Debug::WriteLine("Parent()"); }
        ~Parent() { Debug::WriteLine("~Parent()"); }
        !Parent() { Debug::WriteLine("!Parent()"); }
    };
    
    public ref class Child : public Parent
    {
    public:
        Child() { Debug::WriteLine("Child()"); throw gcnew Exception(); }
        ~Child() { Debug::WriteLine("~Child()"); }
        !Child() { Debug::WriteLine("!Child()"); }
    };
    
    try
    {
        Object^ o = gcnew Child();
    }
    catch(Exception^ e)
    {
        Debug::WriteLine("Exception Caught");
        Debug::WriteLine("GC::Collect()");
        GC::Collect();
        Debug::WriteLine("GC::WaitForPendingFinalizers()");
        GC::WaitForPendingFinalizers();
        Debug::WriteLine("GC::Collect()");
        GC::Collect();
    }
    

    Output:

    Parent()
    Child()
    A first chance exception of type 'System.Exception' occurred in CppCLI-DisposeTest.exe
    ~Parent()
    Exception Caught
    GC::Collect()
    GC::WaitForPendingFinalizers()
    !Child()
    !Parent()
    GC::Collect()
    

    Looking at the Reflector output, here’s how the C++/CLI compiler compiled the Child constructor (decompiling to C# syntax).

    public Child()
    {
        try
        {
            Debug.WriteLine("Child()");
            throw new Exception();
        }
        fault
        {
            base.Dispose(true);
        }
    }
    

    For comparison, here’s the equivalent program in C#.

    public class Parent : IDisposable
    {
        public Parent() { Debug.WriteLine("Parent()"); }
        public virtual void Dispose() { Debug.WriteLine("Parent.Dispose()"); }
        ~Parent() { Debug.WriteLine("~Parent()"); }
    }
    
    public class Child : Parent
    {
        public Child() { Debug.WriteLine("Child()"); throw new Exception(); }
        public override void Dispose() { Debug.WriteLine("Child.Dispose()"); }
        ~Child() { Debug.WriteLine("~Child()"); }
    }
    
    try
    {
        Object o = new Child();
    }
    catch (Exception e)
    {
        Debug.WriteLine("Exception Caught");
        Debug.WriteLine("GC::Collect()");
        GC.Collect();
        Debug.WriteLine("GC::WaitForPendingFinalizers()");
        GC.WaitForPendingFinalizers();
        Debug.WriteLine("GC::Collect()");
        GC.Collect();
        return;
    }
    

    And the C# output:

    Parent()
    Child()
    A first chance exception of type 'System.Exception' occurred in CSharp-DisposeTest.exe
    Exception Caught
    GC::Collect()
    GC::WaitForPendingFinalizers()
    ~Child()
    ~Parent()
    GC::Collect()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While browsing some source code I came across a function like this: void someFunction(char
I was just browsing Sizzle's source code and I came across this line of
While browsing some code, I came across this line: if False: #shedskin I understand
While browsing I came across this blog post about using the Wikipedia API from
Browsing through the source code of Microsoft's sample StockTrader application, I found this snippet
Browsing through the source of a spring framework project i came across a method
Was browsing the jQuery source code when I met this line: jQuery(this)[ state ?
Browsing the code sample from C# 4.0 in a nutshell I came across some
While browsing the code of an erlang application, I came across an interesting design
I was browsing a GNUstep project on github and came across this little loop...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.