C # using usage

Source: Internet
Author: User

The Using 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. Let's take a look at the introduction in this article.

AD: 2014wot Global Software Technology Summit Beijing site course video release

 

In. in the net family, many keywords assume a variety of roles. For example, the New Keyword takes several roles. In addition to being able to create objects, the basic class members are hidden in the inheritance system, the constraints that may be used as type parameters in the generic declaration are also discussed in detail.UsingTo understand the ease and depth of. Net in terms of language mechanisms.

So what are the multiple identities of using reflected in? Let's take a quick look:

1. Introduce the namespace

2. Create an alias

3. Force Resource cleanup

Next, this article will explain the various using applications from these perspectives.

(1) reference a namespace

The usage rules of using as a namespace instruction are as follows:

  1. Using namespace;

In. net programs, the most common code is to introduce the system namespace at the beginning of the program file, because the system namespace encapsulates many of the most basic and most commonly used operations.

About: namespace

The namespace is. the logical structure of the net program, rather than the actual physical structure, is a way to avoid class name conflicts, used to divide different data types. For example, in. net, many basic types are in the system namespace, and the data operation type is in the system. Data namespace,

(2) create a namespace alias

The usage rules for using to create an alias for a namespace are as follows:

  1. Using alias = namespace | type;

Namespace indicates the alias used to create a namespace, and type indicates the alias used to create a namespace. For example.. Net Office applications, Microsoft. office. interOP. word. DLL assembly. To avoid tedious type input when introducing namespaces, we usually create aliases for them as follows:

  1. Using MSWord = Microsoft. Office. InterOP. word;

In this way, Microsoft. Office. InterOP. Word prefix can be replaced by MSWord in the program. If you want to create an application object, this can be the case,
Another benefit is that when different namespaces but the same class names are introduced in a. CS file, aliases can be used to solve this problem.

3) force resource cleanup

Purpose: Clear unmanaged resources that are not controlled by GC. After the using ends, the disposable method is called implicitly.

Usage:

  1. Using (class1 c = new class1 ())
  2. {
  3. } // Clear unmanaged resources not controlled by GC

However, when an object uses the Using Keyword, The idisposable interface must be implemented. In fact, using works the same way as calling the disposable method in the finaly code field in try-catch-finaly. Note that using cannot use multiple different classes.

  1. Class1 F = new class1 ();
  2. Try
  3. {// Run the code
  4. }
  5. Catch ()
  6. {
  7. // Exception Handling
  8. }
  9. Finally
  10. {
  11. F. Disposable ();
  12. }

The Using statement obtains one or more resources, executes one statement, and then processes the resource.

Using statement: Using (Resource Acquisition) embedded statement

Resource Acquisition: local variable Declaration

Expression

A resource is a class or structure that implements system. idisposable. It contains a single non-parameter method named dispose. The code that is using the resource can call dispose to indicate that the resource is no longer needed. If dispose is not called, it will be automatically disposed of due to garbage collection.

If the resource is obtained in the form of a local variable declaration, the declared type of this local variable must be system. idisposable or can be implicitly converted to the system. idisposable type. If the resource is obtained as an expression, the expression must be system. idisposable or be implicitly converted to system. idisposable.

The local variables declared in resource retrieval must be read-only and contain an initial value.

The Using statement is translated into three parts: acquisition, use, and disposal. The use of resources is implicitly enclosed in try statements that contain a finally clause. This finally Clause handles resources. If a null resource is obtained, no call is made to dispose, and no exception is thrown.

For example, the following using statements

  1. Using (r R1 = new R ()){
  2. R1.f ();
  3. }

It is equivalent

  1. R R1 = new R ();
  2. Try {
  3. R1.f ();
  4. }
  5. Finally {
  6. If (R1! = NULL) (idisposable) R1). Dispose ();
  7. }

A simple keyword for a variety of different application scenarios. This article explains the using keyword in. net, it is worth noting that this usage is not implemented in. all advanced languages of. net, the situation of this article is mainly limited to C.

[Reprint] http://developer.51cto.com/art/201105/263550.htm

C # using usage

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.