Function of the Using Keyword

Source: Internet
Author: User

Using Keyword

There are two purposes: Using and using. Where

The Using command has two purposes:

 

(1) The type can be used in the namespace, so that you do not have to limit the type used in the namespace.

 

(2) create an alias for the namespace.

 

Note: The range of the using command is limited to files containing it. Create a using alias to limit the identifier to a namespace or type. Create the using command to use the type in the namespace without specifying the namespace. The Using command does not provide users with access to any namespaces nested in the specified namespace.

 

Using statement:

Defines a range to release one or more objects out of this range.

 

The usage of the using command is very common. Here we will explain how the using statement releases objects. C # Use the. NET Framework Common Language Runtime Library (CLR) to automatically release the memory used to store unnecessary objects. The release of memory is uncertain. Once the CLR decides to execute garbage collection, the memory will be released. However, it is usually best to release limited resources such as file handles and network connections as soon as possible.

UsingThe statement allows the programmer to specify when the resource object should be released..

The object provided for the using statement must implement the idisposable interface.

This interface provides the dispose method, which releases the resources of this object.

 

The Using statement can be used with multiple objects at the same time,

Note that these objects must be declared inside the using statement..


Using (font fontone = new font ("", 10.0f), fonttwo = new font ("Arial", 8.0f ))

{

// You can use the fontone object and fonttwo object

}

// The fontone objects and fonttwo objects cannot be used any more because they are released.

The above is the simple usage of using.

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.