C # using usage

Source: Internet
Author: User

(1) The usage rule for referencing the Namespace using as the Namespace instruction is: using Namespace. NET programs, the most common code is to introduce the System namespace at the beginning of the program file, the reason is that the System namespace encapsulates many of the most basic and most commonly used operations, the following code is most familiar to us: using System; in this way, we can directly use the type in the namespace in the program without specifying the detailed type name. The using command can access the nested namespace. About: 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 using. The usage rule for creating an alias for a namespace is: using alias = namespace | type. namespace indicates the alias for creating a namespace; type indicates creating a type alias. 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. in this way, Microsoft can be replaced by MSWord in the program. office. interop. the Word prefix. If you want to create an Application object, this can be the case. Another benefit is that. the alias can be used to solve this problem when different namespaces but the same class names are introduced in the CS file. (3) force resource cleanup purpose: Clear resources that are not controlled by GC. After the Using ends, the Disposable method is called implicitly. Usage: using (Class1 c = new Class1 () {} // clears unmanaged resources that are 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 {// Execution 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: Partial variable declaration expression resource is a class or structure that implements System. IDisposable. It contains a single non-parameter method named Dispose. (Example: 2) 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 statement using (R r1 = new R () {r1.F ();} is equivalent to R r1 = new R (); try {r1.F ();} finally {if (r1! = Null) (IDisposable) r1). Dispose ();} several using usage 1. using command. Using + namespace name, so that you can directly use the type in the Command Space in the program, without specifying the type of detailed namespace, similar to the Java import, this function is also the most commonly used, almost every cs program is used. For example, using System; usually appears in *. cs. 2. using alias. Using + alias = contains the specific type of detailed namespace information. This method has the advantage that when the same cs references two different namespaces, but both namespaces contain the same name type. When this type is required, the detailed namespace method should be used in every place to distinguish the types with the same name. The alias method is more concise. You can declare the alias for the class you use. Note: This does not mean that the two names are repeated. If you use an alias for one of them, you do not need to use the alias for the other. If you want to use both of them, you need to use using to define the alias for both of them. For example:

Using System; using aClass = NameSpace1.MyClass; using bClass = NameSpace2.MyClass; namespace NameSpace1 {public class MyClass {public override string ToString () {return "You are in NameSpace1.MyClass ";}}} namespace NameSpace2 {class MyClass {public override string ToString () {return "You are in NameSpace2.MyClass" ;}} namespace testUsing {using NameSpace1; using NameSpace2; ///Cl Ass1 abstract description. /// Class Class1 {///// main entry point of the application. /// [STAThread] static void Main (string [] args) {// TODO: Add code here to start the application // aClass my1 = new aClass (); console. writeLine (my1); bClass my2 = new bClass (); Console. writeLine (my2); Console. writeLine ("Press any key"); Console. read () ;}} 3. the using statement defines a range to process objects at the end of the range. Scenario: When a class instance is used in a code segment, you want to automatically call the Dispose of the class instance if you leave the code segment for whatever reason. To achieve this goal, it is also possible to catch exceptions using try... catch, but it is also convenient to use using. Example: using (Class1 cls1 = new Class1 (), cls2 = new Class1 () {// the code using cls1, cls2} // call the Dispose on cls1 and cls2. Here, the Dispose condition that triggers cls1 and cls2 is that it reaches the end of the using statement or causes an exception in the middle of the process and controls to exit the statement block.

Related Article

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.