C ++/CLI: automatic loading of idisposable: dispose

Source: Internet
Author: User
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.