C # using three ways to use

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/fashui/archive/2011/09/29/2195061.html

1.using instructions. Using + namespace name, so that you can directly use the type in the command space in the program, without specifying a type of verbose namespace, similar to Java import, this function is the most common, almost every CS program will be used.
Example: Using System; Usually appear in the *.cs.

2.using aliases. Using + aliases = Specific types that include detailed namespace information.
One advantage of this approach is that when the same CS references two different namespaces, but two namespaces include a type of the same name. When this type is needed, it is necessary to distinguish the types of the same names in each place using the detailed namespace method. The alias method will be more concise, which class will be used to give the class alias declaration. Note: Not that two names are duplicated, one is aliased, the other does not need to be aliased, and if two are to be used, two of them need to use a using to define the alias.

1 usingSystem;2 usingAClass =Namespace1.myclass;3 usingBclass =Namespace2.myclass;4  5 namespaceNameSpace16 {7      Public classMyClass8     {9          Public Override stringToString ()Ten         { One             return "You is in namespace1.myclass"; A         } -     } - } the   - namespaceNameSpace2 - { -     classMyClass +     { -          Public Override stringToString () +         { A             return "You is in namespace2.myclass"; at         } -     } - } -   - namespacetestusing - { in     usingNameSpace1; -     usingNameSpace2; to     /// <summary> +     ///a summary description of the CLASS1.  -     /// </summary> the     classClass1 *     { $         /// <summary>Panax Notoginseng         ///The main entry point for the application.  -         /// </summary> the [STAThread] +         Static voidMain (string[] args) A         { the             // +             //TODO: Add code here to launch the application -             // $   $AClass my1 =NewAClass (); - Console.WriteLine (my1); -Bclass My2 =NewBclass (); the Console.WriteLine (my2); -Console.WriteLine ("Press any key");Wuyi Console.read (); the         } -     } Wu}

A 3.using statement that defines a range that handles objects at the end of a range.
Scene:
When an instance of a class is used in a code snippet, it is hoped that, for whatever reason, the dispose of this class instance will be called automatically whenever you leave this code snippet.
To achieve this goal, it is also possible to use try...catch to catch anomalies, but it is also convenient to use using.

1 using New New Class1 ()) 2 {3   // the code using CLS1, CLS2 4 // Call the Dispose on CLS1 and Cls2

C # using three ways to use

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.