There is a critical keyword in net, which is the using
Using generally has the following uses:
1. Direct introduction of namespaces
A, using System, this is the most commonly used, is the using+ namespace, so you can directly use the type in the namespace, but eliminates the use of detailed namespaces
b, using fully qualified names
Do not use using System; Call System.Console.WriteLine ("Hello C #") directly in the program;
The first method is a more common method, which makes it easy to import the entire namespace to the current namespace at a time.
However, it is appropriate to use only one class in the namespace in the current namespace, such as a fully qualified name in the previous example.
In some cases, however, you must use a fully qualified name, such as a class with the same name in more than two namespaces that are introduced, such as a console class under System, and a console class with the same names in another custom namespace MyNamespace. Then if we introduce system and MyNamespace in the third namespace, then if we want to use a specific Console, we need the right to qualify the name System.Console or Mynamespace.console, otherwise the compiler does not know which Console we use specifically, the compilation cannot pass.
2.using alias. A using + alias = A specific type that includes detailed namespace information.
For example, we introduce the System.IO.Compression namespace with the following statement:
Using Zip=system.io.compression;
Then we can use a zip to represent the System.IO.Compression namespace, using Zip.gzipstream is to use System.IO.Compression.GZipStream. It is convenient to write the program.
One advantage of this approach is that when the same CS references two different namespaces, the two namespaces include a type with the same name. When this type is needed, it is necessary to use a detailed namespace approach to distinguish the types of these same names everywhere. The method of using aliases will be more concise, which class you use to make an alias declaration on which class. Note: Not that two names are duplicated, one is aliased, the other does not need to use aliases, and if two are to be used, then two will need to use to define aliases.
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;
///
/// Class1 的摘要说明。
///
class Class1
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
aClass my1 = new aClass();
Console.WriteLine(my1);
bClass my2 = new bClass();
Console.WriteLine(my2);
Console.WriteLine("Press any key");
Console.Read();
}
}
}
3.using statement that defines a range that handles objects at the end of the range.
Scene:
When you use an instance of a class in a code snippet, you want to automatically call dispose of the class instance as long as you leave the code snippet for whatever reason. To achieve this, it is also possible to use Try...catch to catch exceptions, but it is also convenient to use using the.
For example:
using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
// the code using cls1, cls2
} // call the Dispose on cls1 and cls2
The dispose condition that triggers cls1 and cls2 here is to reach the end of the using statement or to cause an exception to be thrown and control to leave the statement block