• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

Dispose Pattern in C# vs C++

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)
    {
    }
};


Use C# to C++ Converter to convert from C# to C++.     

Additional resource: C++ and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.