Using a using-free resource

Source: Internet
Author: User

What statements can use the using management?

Analysis: scarce resources require using management.

is the using release a managed or unmanaged resource?

So what are managed and unmanaged resources?

managed resources are resources that are allocated and freed by the CLR, typically managed memory, while unmanaged resources are resources that are allocated and freed by the system. In general, the new object in the CLR or the allocation of an array does not need to be manually disposed of within, such as the handle resource in Windows, is often required to be released manually, such as fonts, brushes, DC, and all window kernel objects (handles) are unmanaged resources such as file handles, socket handles, form handles ... Too much.The memory that the new object occupies is a managed resource.

Parsing: unmanaged ,C # apps hosted into the . NET Framework. But he can release unmanaged resources.

Two. The role of the using statement

Parse: as a keyword, using can import namespaces

As a C # statement, the using can free the memory resources occupied by the object, with the following basic syntax

using (SqlConnection con=new SqlConnection ("")) {    // database operation code }

Three. The nature of the using statement

Parsing: (Since the using can release the object, is it possible to use a using to restrict all objects before it?) )

Not so;

Using an object that can be actively freed requires the implementation of the Idisable interface

Now that we've implemented the idisable excuse, there's no need to use using all of them, we're only using qualified objects that have a significant impact on system performance, and other objects can be handed off to the Akira recovery processor.

Four. The Dispose method automatically calls Close ()

For a type to be managed by using , the type or parent class must implement the IDisposable interface.

using (SqlConnection con=new  SqlConnection) {   // essentially automatically calls the Dispose method }

1.close () and Dispose () difference?

Parse: Close() just closes the connection, but the channel is not destroyed,andDispose () not only closes the connection, but also destroys the channel.

Using a using-free resource

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.