Function of using in C # -- release resources

Source: Internet
Author: User

I'm glad to see a knowledge point that can be shared today, that is, the use of using.

In fact, the role of using is the most widely used namespace. :) In fact, I used to be the same as before, but today I am reading a small example, And I have doubts. Haola, I will attach the code first.

Using system; <br/> using system. io; </P> <p> class test <br/>{< br/> Public static void main () <br/>{< br/> try <br/> {<br/> // create an instance of streamreader to read from a file. <br/> // The using statement also closes the streamreader. <br/> using (streamreader sr = new streamreader ("testfile.txt") <br/>{< br/> string line; <br/> // read and display lines from the file until the end of <br/> // The file is reached. <br/> while (line = Sr. Readline ())! = NULL) <br/>{< br/> console. writeline (line); <br/>}< br/> catch (exception E) <br/>{< br/> // Let the user know what went wrong. <br/> console. writeline ("the file cocould not be read:"); <br/> console. writeline (E. message); <br/>}< br/>

I don't know if there is one of the above programs that makes you confused. haola, I won't sell it. What I don't know is as follows:

Using (streamreader sr = new streamreader ("testfile.txt "))

{...}

At first, I thought it was a namespace or something, but with a skeptical attitude, I went to the Q group to raise my own questions. With their help, I was able to correct my previous mistakes. In fact, we often use the using syntax when connecting to the database, similar to the following sentence:
Using (sqlconnection conn = newSqlconnection (source ))
{
// Code
}

Never think that the instantiation in any place can be used in the using block like this,It is used only when resources need to be released automatically.. In fact, you may not understand the need to release resources automatically. To use the using block in this way, make sure that the following conditions are met:

(1) This class implements the interfaceIdisposable(This interface only has one method.Void dispose() When this class is instantiated in using, when the using code block ends, it will automatically call the dispose () method that implements the idisposable interface in this class.The dispose () method is often used to release resources..

Let's take a look at the following simple example:

Using system; </P> <p> class Program <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> using (test = new test () <br/>{< br/> console. writeline ("disposable is open! "); <Br/>}</P> <p> console. writeline (" disposable is none! "); <Br/> console. readkey (); <br/>}</P> <p> public class test: idisposable <br/>{</P> <p> # region idisposable member </P> <p> Public void dispose () <br/>{< br/> console. writeline ("disposable is close! "); <Br/>}</P> <p> # endregion <br/>}

The output result is as follows:

Disposable is open!
Disposable is close!
Disposable is none!

Haola, today's writing is here. Thank you!

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.