1.using instructions. Using + namespace name, so that you can directly use the type in the command space in the program, without specifying a type of verbose namespace, similar to Java import, this function is the most common, almost every CS program will be used.
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.
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
usingSystem;usingaClass = NameSpace1.MyClass;using bClass = NameSpace2.MyClass;namespaceNameSpace1 { publicclassMyClass { publicoverridestring ToString() { return"You are in NameSpace1.MyClass"; } }}namespaceNameSpace2 { classMyClass { publicoverridestringToString() { return"You are in NameSpace2.MyClass"; } }}namespacetestUsing{ using NameSpace1; usingNameSpace2; /// <summary> /// Class1 的摘要说明。 /// </summary> classClass1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] staticvoidMain(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // aClass my1 = new aClass(); Console.WriteLine(my1); bClass my2 = newbClass(); 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.
| 1234 |
using(Class1 cls1 = new Class1(), cls2 = newClass1()){ // the code using cls1, cls2} // call the Dispose on cls1 and cls2 |
C # using three ways to use