CSharp Keyword -- using

Source: Internet
Author: User

There is a crucial keyword in. net, that is, using.

Using generally has the following usage:

1. Directly introduce the namespace

A. using System. This is the most commonly used namespace, namely, using + namespace. In this way, you can directly use the types in the namespace without using the detailed namespace.

B. Use a fully qualified name
You do not need to use using System; directly call System. Console. WriteLine ("Hello C #") in the program #");

The first method is a commonly used method. It is convenient to import the entire namespace to the current namespace at a time.
However, if only one class in the namespace is used in the current namespace, for example, it is also appropriate to use a fully qualified name in the above example.

However, in some cases, you must use a fully qualified name. For example, if two or more namespaces are introduced with duplicate names, for example, there is a Console class under System, in another custom namespace, MyNamespace also has a Console class with the same name. If System and MyNamespace are introduced in the third namespace at the same time, at this time, if you want to use a specific Console, you need to use the name System. console or MyNamespace. console. Otherwise, the compiler does not know which Console we use. Compilation fails.

2. using alias. Using + alias = contains the specific type of detailed namespace information.
For example, we use the following statement to introduce the System. IO. Compression namespace:
Using Zip = System. IO. Compression;
In this case, we can use Zip to represent the System. IO. Compression namespace. The use of Zip. GZipStream is System. IO. Compression. GZipStream. It facilitates program writing.

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;    class Class1    {        [STAThread]        static void Main(string[] args)        {            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 as long as 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.

For 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 for triggering cls1 and cls2 is to reach the end of the using statement or cause an exception in the middle and control to exit the statement block.

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.