Several usage

xiaoxiao2021-03-06  54

The inventory. Using namespace name, this can be directly used in the program, without having to specify the detailed namespace of the type, similar to Java's import, this feature is also the most common, almost every CS program will be used . For example: use system; generally appears in * .cs. 2.USING alias. USING alias = Specific types including detailed namespace information. This approach has a good thing to use two different namespaces, but two namespaces include a type of the same name. When this type is needed, you must use a detailed namespace to distinguish the type of these same names. The method of using alias will be more concise. Which class is used to do an alias declaration. Note: It is not to say that two names are repeated, give one of the alias, and the other is not needed, if both are used, then both need to define alias. E.g:

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 Summary Description. /// class class1 {/// /// The primary entry point of the application. /// [stathread] static void main (string [] args) {// // Todo: Add code here to start the application // aclass my1 = new aclass (); console.writeLine (MY1 ); Bclass my2 = new bclass (); console.writeline (my2); console.writeline ("press any key"); console.read ();}}}

3.using statement, define a range, processing the object at the end of the range. Scenario: Use the instance of the class in a code segment, and I hope that regardless of the reason, the Dispose of this class instance is automatically called whenever you leave this code segment. To achieve this, use try ... catch to capture an exception, but it is also very 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 of CLS1 and CLS2 is to reach the end of the USING statement or the middle trip to abnormally and controlled the statement block.

转载请注明原文地址:https://www.9cbs.com/read-116461.html

New Post(0)