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 #