Use of the Using keyword

Source: Internet
Author: User

Two functions of the using statement:

1) using can import namespaces

2) The using can release the memory resources occupied by the object.

The code is as follows:

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

The using statement allows the user to define a range and dispose of the object at the end of the range. A database connection object is defined in the using statement, and when the program executes at the end of the using statement, the CLR immediately releases the object by calling the object's Dispose () method.

is the using release a managed or unmanaged resource?

Analytical:

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

Using substance
During the program compilation phase, the compiler automatically builds the using statement into a try-finally statement and calls the object's Dispose method in the finally block to clean up the resource. Therefore, the using statement is equivalent to the try-finally statement

What is the difference between close () and Dispose ()?

Close () Just closes the connection, but the channel is not destroyed, and Dispose () not only shuts down the connection, but also destroys the channel.

Use of the Using keyword

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.