Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

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#:

public class DisposePattern: IDisposable
{
        ~DisposePattern()
        {
                this.Dispose(false);
        }
        public void Dispose()
        {
                this.Dispose(true);
        }
        protected override void Dispose(bool disposing)
        {
        }
}

C++/CLI:

public ref class DisposePattern : IDisposable
{
private protected:
        !DisposePattern()
        {
                this->DisposeObject(false);
        }
public:
        ~DisposePattern()
        {
                this->DisposeObject(true);
        }
private:
        void DisposeObject(bool disposing)
        {
        }
};

If you need to convert from C# or VB to C++ and you are depending on the results being reliable and accurate, then you will want to have C# to C++ Converter or VB to C++ Converter at your fingertips.

Copyright © 1997 - 2017 Tangible Software Solutions Inc.