Several usage

xiaoxiao2021-03-06  38

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 main entry point for 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.

Scenes:

When you use an instance of a class in a code segment, you want to automatically call this class of Dispose if you leave this code segment, regardless of whether this code segment is left.

To achieve this, use try ... catch to capture an exception, but it is also very convenient to use using.

E.g:

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-56161.html

New Post(0)