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:
- 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:
- 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:
- 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:
- Using (class1 c = new class1 ())
- {
- } // 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.
- Class1 F = new class1 ();
- Try
- {// Run the code
- }
- Catch ()
- {
- // Exception Handling
- }
- Finally
- {
- F. Disposable ();
- }
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
- Using (r R1 = new R ()){
- R1.f ();
- }
It is equivalent
- R R1 = new R ();
- Try {
- R1.f ();
- }
- Finally {
- If (R1! = NULL) (idisposable) R1). Dispose ();
- }
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