Dispose Pattern in C# vs C++





























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.NET to C++/CLI and you are depending on the results being reliable and accurate, then you will want to have Instant C++ C# Edition or Instant C++ VB Edition at your fingertips.

 

 
 
Email your questions and comments to:  info@tangiblesoftwaresolutions.com

[Site Map]        [Home]


Copyright © 1997 - 2008 Tangible Software Solutions Inc.