Using three ways to use the C # learning notes

Source: Internet
Author: User
Tags aliases

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.

Using system;using AClass = namespace1.myclass;using Bclass = Namespace2.myclass; Namespace namespace1{public class MyClass {public override string ToString () {return ' Yo        U is in Namespace1.myclass "; }}} namespace namespace2{class MyClass {public override string ToString () {return ' Y        OU is in Namespace2.myclass ";    }}} namespace testusing{using NameSpace1;    Using NameSpace2;    <summary>///CLASS1 Summary description.        </summary> class Class1 {///<summary>//The main entry point of the application. </summary> [STAThread] static void Main (string[] args) {/////TODO:            Add code here to launch the application//AClass my1 = new AClass ();            Console.WriteLine (MY1);            Bclass my2 = new Bclass ();            Console.WriteLine (MY2);            Console.WriteLine ("Press any Key");        Console.read ();   } }} 

  


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.

using (Class1 cls1 = new Class1 (), cls2 = new Class1 ()) {  //The code using CLS1, CLS2}//Call the Dispose on CLS1 and Cls2

  


Using three ways to use the C # learning notes

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.