Using usage pick up memory

Source: Internet
Author: User

The using usage consists of three main types:

1. Referencing external namespaces and types defined in external namespaces (directives)

2. Create a namespace alias to avoid conflicts (directives) caused by the same name

3. Define resource Usage scope, Release resource object (statement) after scope end


1. Refer to an external namespace or type, allowing reference namespace types to be used within this namespace:

Using System.IO;
Using System.Linq;
Using System.Text;

2. Create a namespace alias that distinguishes the same name namespace or type:

Using txt= System.Text;

Using Com=common.log;

Using Model=model.log;

Distinguishing between the same types under different namespaces can also be referenced by writing a full name, although the direct reference to the alias is more concise.

3.using statement block, you can release the resource object as needed in a timely manner. It is important to note that the resource object defined by the using statement block must implement the IDisposable interface.

To destroy a resource object by using the Dispose function to release:

A single Resource object

Pen pen=new Pen (brushes.red)

using (pen)
{
}

using (Pen pen=new pen (brushes.red))
{
}

Multiple resource objects of the same type

using (Pen pen=new pen (brushes.red), Blackpen=new Pen (brushes.black))
{
}

Multi-type Multiple resource objects

using (IDisposable pen = new Pen (brushes.red), image = new FileStream (@ "D:\file.txt", FileMode.Create))
{
}

Note: When a single Resource object is declared, the declaration of an object can be within a using or externally declared object, and multiple resource objects must be declared within the using.

And the using will check the type at compile time, must ensure the object type is consistent; multiple types of resource objects need to be declared as IDisposable types, initialized to use

The different types of resource objects;

The substance of the using statement block is equivalent to try: Finally statement block, because the code converts the using statement block to a try...finally statement block during compilation, and the resource object

Destruction is done by calling IDisposable's Dispose function within Finally, so the object type must inherit the IDisposable interface. Using a using statement block

It is important to be aware that you cannot nest a using statement block in multiple layers, because a bug in the inner statement block can cause the outer using of the resource to be deallocated. This usage scenario should be selected using the

Try...finally the destruction of resource objects is better.

Try

{

}

Finally

{

Resource destruction;

}

Using usage pick up memory

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.