. Net, have you forgotten? (2) -- use using to clear unmanaged Resources

Source: Internet
Author: User

We all know that garbage collection can be divided into two types: dispose and finalize. There are too many differences between the two. One is the method called by the normal garbage collection GC, and the other is the finalizer, the called method has a clear suggestion in Objective C # That the idispose interface be used instead of finalize. The reason is that the finalize termination will increase the algebra of the garbage collection object, thus affecting garbage collection.

With the above reasons, we can only look at the classes using the idispose interface.

In. net, the vast majority of classes are running in a hosted environment, so GC is responsible for collection. Therefore, we do not need to implement the idispose interface, but GC is automatically responsible. However, some classes use non-hosted resources. At this time, we should implement the idispose interface, which is a commonly used sqlconnection.

Model of commonly used SQL statements for writing segments:

StringConnectionstring = system. configuration.Configurationmanager. Connectionstrings ["Study1connectionstring1"]. Connectionstring;SqlconnectionThisconnection =NewSqlconnection(Connectionstring); thisconnection. open ();SqlcommandThiscommand =NewSqlcommand(); Thiscommand. Connection = thisconnection; thiscommand. commandtext ="Select * from [user]"; Thiscommand. executenonquery (); thisconnection. Close ();

In fact, as an unmanaged resource, finalize is generally implemented to prevent us from calling close. Therefore, even if we do not close it, The Terminator will reclaim the memory. However, it adds the garbage algebra.

Suppose we have written thisCode:

StringConnectionstring = system. configuration.Configurationmanager. Connectionstrings ["Study1connectionstring1"]. Connectionstring;SqlconnectionThisconnection =NewSqlconnection(Connectionstring); thisconnection. open ();SqlcommandThiscommand =NewSqlcommand(); Thiscommand. Connection = thisconnection; thiscommand. commandtext ="Select * Form [user]"; // SQL statement error thiscommand. executenonquery (); thisconnection. Close ();

In this case, the sqlconnection we opened is not closed, and we can only wait for finalize to close it.

This is a very bad practice. So we can think of exception handling:

 Sqlconnection Thisconnection = Null ; Try { String Connectionstring = system. configuration. Configurationmanager . Connectionstrings [ "Study1connectionstring1" ]. Connectionstring; thisconnection = New  Sqlconnection (Connectionstring); thisconnection. open (); Sqlcommand Thiscommand = New  Sqlcommand (); Thiscommand. Connection = thisconnection; thiscommand. commandtext ="Select * Form [user]" ; Thiscommand. executenonquery ();} Finally { If (Thisconnection! = Null ) {Thisconnection. Close ();}}

This is good, but the code looks ugly, but the use of using makes the code much more elegant, which is also a lot better for C # than Java!

StringConnectionstring = system. configuration.Configurationmanager. Connectionstrings ["Study1connectionstring1"]. Connectionstring;Using(SqlconnectionThisconnection =NewSqlconnection() {Thisconnection. open ();SqlcommandThiscommand =NewSqlcommand(); Thiscommand. Connection = thisconnection; thiscommand. commandtext ="Select * Form [user]"; Thiscommand. executenonquery ();}

 

Is the amount of code much smaller? Elegant!

In fact, in the location of Il, the code is still the same. He also compiled the code into a try-finally processing form!

Next, let's take a look at the common database usage methods:

StringConnectionstring = system. configuration.Configurationmanager. Connectionstrings ["Study1connectionstring1"]. Connectionstring;SqlconnectionThisconnection =NewSqlconnection(Connectionstring); thisconnection. open ();SqlcommandThiscommand =NewSqlcommand(); Thiscommand. Connection = thisconnection; thiscommand. commandtext ="Select * from [user]";SqldatareaderThisreader = thiscommand. executereader (); thisreader. Close (); thisconnection. Close ();

 

Or the above problem, we consider using the using statement to refactor the Code:

 String Connectionstring = system. configuration. Configurationmanager . Connectionstrings [ "Study1connectionstring1" ]. Connectionstring; Using ( Sqlconnection Thisconnection = New  Sqlconnection (Connectionstring) {thisconnection. open (); Sqlcommand Thiscommand = New  Sqlcommand (); Thiscommand. Connection = thisconnection; thiscommand. commandtext = "Select * from [user]" ;Using ( Sqldatareader Reader = thiscommand. executereader ()){ While (Reader. Read ()){ // Operation }}}

I will first translate this code into a try-finally processing format that we are familiar:

 Sqlconnection Thisconnection = Null ; Try { String Connectionstring = system. configuration. Configurationmanager . Connectionstrings [ "Study1connectionstring1" ]. Connectionstring; thisconnection = New  Sqlconnection (Connectionstring); thisconnection. open (); Sqlcommand Thiscommand = New  Sqlcommand (); Thiscommand. Connection = thisconnection; thiscommand. commandtext = "Select * from [user]" ; Sqldatareader Reader = Null ; Try {Reader = thiscommand. executereader (); While (Reader. Read ()){// Operation }} Finally {Reader. Close ();}} Finally {Thisconnection. Close ();}

More ugly code! Therefore, we recommend that you avoid the nesting of using statements.

How can this problem be solved? It's easy to write our try-Finally!

 Sqlconnection Thisconnection = Null ; Sqldatareader Reader = Null ; Try { String Connectionstring = system. configuration. Configurationmanager . Connectionstrings ["Study1connectionstring1" ]. Connectionstring; thisconnection = New  Sqlconnection (Connectionstring); thisconnection. open (); Sqlcommand Thiscommand = New  Sqlcommand (); Thiscommand. Connection = thisconnection; thiscommand. commandtext = "Select * from [user]" ; Reader = thiscommand. executereader (); While (Reader. Read ()){ // Operation }} Finally {If (Thisconnection! = Null ) {Thisconnection. Close ();} If (Reader! = Null ) {Reader. Close ();}}

That's all!

I wrote this section about using, and finally summarized the full text in the following sentence: Use Using to reclaim the resources of unmanaged resources as much as possible.

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.