Improve system performance: Starting with the data access layer

Source: Internet
Author: User
Tags finally block

Using a using-free resource

(1) using used to release an object occupies an important resource, not only the database object, as well as the file read and write objects;

(2) objects that use the using syntax need to implement the Dispose () method of the Idisable interface.

This method is designed to release important resources that the object occupies.

(3) The Dispose () method encapsulates the call to the close () method.

What's the resource?

A resource is a class or struct that implements System.IDisposable that contains a single parameterless method named Dispose that is using the resource's code to call Dispose to indicate that the resource is no longer needed. If you do not call Dispose, the automatic disposition will eventually occur because of garbage collection.

If the resource acquisition is in the form of a local variable declaration, the type of this local variable declaration must be System.IDisposable or a type that can be implicitly converted to System.IDisposable. If the resource gets in the form of an expression, the expression must be system.idisposable or a type that can be implicitly converted to System.IDisposable.

Local variables declared in resource acquisition must be read-only and must contain an initializer.

The using statement is translated into three parts: acquisition, use, and disposition. The use of a resource is implicitly enclosed in a try statement that contains a finally clause. This finally clause disposes of the resource. If a null resource is obtained, no call to Dispose is made and no exception is thrown.

For example, the following form of a using statement

using (r r1 = new R ()) {
R1. F ();
}
is exactly equivalent to

R r1 = new R ();
try {
R1. F ();
}
finally {
if (r1! = null) ((IDisposable) R1). Dispose ();
}

Two functions of using:

Role 1: Import namespaces

Role 2: Release the resources occupied by the object, such as Connection,datareader,filestream

The scope of the using-induced object is only in curly braces, and the {} does not work anymore.

Dispose:

. 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.

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

}

What statements can use the using management??

Scarce resources require using management.

Using statement methods

It is common to unmanaged resources (that is, the handles and device contexts that may be encountered in C + + programming), which must be freed after the resources are used. In C #, because of the complete object-oriented programming, these resource types implement the IDisposable interface, so the resources are freed using the finally block that captures the exception Try...catch statement, which is the following code:

    1.   font font1  = new  font (" Arial ",  10.0f);  
    2.   try  
    3.   { 
    4.     byte  CharSet  = font1 . gdiCharSet;  
    5.   }  
    6.   finally  
    7.  & nbsp { 
    8.     if  (font1 != null)  
    9.        ((IDisposable) font1). Dispose ();  
    10.   }  

This ensures that dispose is called so that resources are freed. (Note that using extra curly braces creates a valid range for an object, and if you use a using statement at this point, you can also get the same result with the following code:

    1. using (Font font3 = new font ("Arial", 10.0f), font4 = new font ("Arial" , 10.0f))
    2. {
    3. //Use Font3 and font4.
    4. }

The using statement invokes the Dispose method on the object in the correct way, and (when you use it as shown earlier) causes the object itself to be out of scope when you call Dispose. In a using block, the object is read-only and cannot be modified or reassigned.

It is also important to note that:

By rule, this object should be declared and instantiated in the using statement when the IDisposable object is used. Although you can instantiate a resource object outside of the using statement, the code is as follows:

    1. Font Font2 = new font ("Arial", 10.0f);
    2. using (FONT2)//Not recommended
    3. {
    4. Use Font2
    5. }
    6. Font2 is still in scope
    7. But the method call throws an exception
    8. Float F = font2.  GetHeight ();

In this case, the object will remain in scope after control leaves the using block, even if it may no longer have access to its unmanaged resources. In other words, the object can no longer be fully initialized. If you attempt to use the object outside the using block, you may cause an exception to be thrown, such as Font2 in code snippet 3 above. GetHeight () is called and an exception is generated. For this reason, it is usually better to instantiate the object in the using statement and limit its scope to the using block.

Improve system performance: Starting with the data access layer

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.