• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

Dispose Pattern in C# vs C++/CLI

When converting from C# to C++/CLI, note that the C# finalizer  ~ <class name> is converted to the C++/CLI syntax ! <class name> and the Dispose() method is converted to the C++/CLI destructor ~ <class name>.

In the following example, since you can't name methods Dispose in C++/CLI, the C# Dispose(bool) method is just renamed to a private C++/CLI DisposeObject(bool) method for conversion clarity.

C# C++/CLI
public class DisposePattern: IDisposable
{
    ~DisposePattern()
    {
        this.Dispose(false);
    }
    public void Dispose()
    {
        this.Dispose(true);
    }
    protected override void Dispose(bool disposing)
    {
    }
}
public ref class DisposePattern : IDisposable
{
private protected:
    !DisposePattern()
    {
        this->DisposeObject(false);
    }
public:
    ~DisposePattern()
    {
        this->DisposeObject(true);
    }
private:
    void DisposeObject(bool disposing)
    {
    }
};

Additional resource: C++ and C# Equivalents


Copyright © 2004 – 2019 Tangible Software Solutions, Inc.