In C #, the dispose () method must be composed ProgramIf you forget to do so, Your unmanaged resources will not be released until GC recycles the object. In comparison, the destructor in C ++ looks more elegant: After the Object exits the scope, it automatically calls the destructor.
Although there are also destructor in C #, it is actually a finalize () method. We all know that the finalize () method has many problems.
In C ++/CLI (managed C ++ in vs2005), The Destructor will be mapped to the idisposable: dispose () method by the compiler. In addition, as in native C ++, after an Object exits the scope, it will automatically call the destructor, that is, the dispose () method. This is undoubtedly a very considerate design.
Ref Class
{
...
~ A ()
};
Void Foo ()
{
A A; // modified to the new syntax 2004.10.16
...
} // Here, A. Dispose will be called
AboveCodeThe function is like the usage of using in C:
Private void Foo ()
{
Using (A = new ())
{
....
} // Here, A. Dispose will be called
}