Usage of using in C #

Source: Internet
Author: User

Usage of using in C #

1. Allows a type to be used in a namespace so that it does not have to be qualified in that namespace (indicating the specific source of that type) of the use of a type.

Syntax: using+ namespace (using System)

2. Allow access to static members of the type (static member functions and static data members) without qualifying access using the type name.

Syntax: using static + namespace + type (using static System.Math) (such as accessing sqrt in the Math class (..) static method that can be used directly with SQRT (..) method without having to use MATH.SQRT (...) This form).

3. Create an alias name for the namespace or type. This is known as a using alias Directive .

Benefits of creating aliases:

The same CS references two different namespaces, but two namespaces all 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 introduction of aliases can solve this kind of situation.

How to create an alias:

Using TestA = Namespacea.testclass;
Using TESTB = Namespaceb.testclass;

This allows you to distinguish which namespace the TestClass comes from.

4. Use using as a statement

using New Font ("Arial"10.0f)) {    byte charset = font1. gdiCharSet;}

The type in the using must implement the IDisposable interface, which is equivalent to the following statement:

{  new Font ("Arial"10.0f);   Try   {    byte charset = font1. gdiCharSet;  }   finally   {    ifnull)      ((IDisposable) font1). Dispose ();  }}

Usage of using in C #

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.