Visual C++ .NET/Class/IDisposable

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

implements IDisposable

 
#include "stdafx.h"
#include <msclr\auto_handle.h>
using namespace System;
using namespace msclr;
ref class MyObject : public IDisposable
{
private:
    String^ Name;
public:
    MyObject(String^ name) : Name(name) 
    { 
        Console::WriteLine("Constructor - {0}", Name); 
    }
    ~MyObject() 
    { 
        Console::WriteLine("Destructor  - {0}", Name); 
        Name = nullptr;
    }
protected:
    !MyObject() 
    { 
        Console::WriteLine("Finalize    - {0}", Name);  
        Name = nullptr;
    }
};

int main(array<System::String ^> ^args)
{
    {
        MyObject^ NonAuto = gcnew MyObject("Non Auto no finally");
    }
    MyObject^ NonAutoWFinally;
    try
    {
        NonAutoWFinally = gcnew MyObject("Non Auto with finally");
    }
    finally
    {
        delete NonAutoWFinally;
    }
    {
        auto_handle<MyObject> Auto = gcnew MyObject("Auto");
    }
    return 0;
}