Namespace
NamespaceA keyword is used to declare a range. This namespace range allows you to organize code and provides you with a method to create a globally unique type:
- The namespace name can be any legal identifier. The namespace name can contain periods.
- The default namespace is created even if the namespace is not explicitly declared. This unnamed namespace (sometimes called a global namespace) exists in each file. Any identifier in the global namespace can be used in the namespace.
- The namespace has implicit public access permissions, and this is unchangeable.
- You can define a namespace in two or more declarations. For example, two classes are defined in the same file
MyCompany
Part of the namespace:
- // Keywords_namespace.cs
Namespace Hunts. Test
{
Class MyClass1
{
//
}
}
Namespace Hunts. Test
{
Class MyClass2
{
//
}
}
Example: (omitted)
Using
UsingThe keyword has two main purposes:
- This command is used to create an alias for a namespace or import the types defined in other namespaces.
Using namespace;
Using alias = type | namespace;
// Type: the Type you want to use alias.
This method can be used to handle the conflicts that need to be used when the namespace of the class containing the same name is referenced at the same time.(See example 1)
// Namespace: namespace that you want to use alias. Or a namespace that contains the type you want to use without specifying a fully qualified name.
- As a statement to define a range. objects are released at the end of this range.
- Font myFont = new Font ("Arial", 10.0f );
Using (myFont)
{
// Use myFont
}
Example:
- The following example shows how to define the using command and using alias for the class:
- // Using Keyword
// Keywords_using.cs
Using System;
Namespace N1
{
Class
{
Public void Test ()
{
//
}
}
}
Namespace N2
{
Class
{
Public void Test ()
{
//
}
}
}
Namespace Hunts. Keywords
{
// General Practice
// Using N1;
// Using N2;
Using type1 = N1.A;
Using type2 = N2.A;
Class App
{
Static void Main ()
{
// General Practice
// N1.A c1 = new N1.A ();
// N2.A c2 = new N2.A ();
Type1 t1 = new type1 ();
Type2 t2 = new type2 ();
//
}
}
}
Note:
- C # Use the. NET Framework Common Language Runtime Library (CLR) to automatically release the memory used to store unnecessary objects. The release of memory is uncertain. Once the CLR decides to execute garbage collection, the memory will be released. However, it is usually best to release limited resources such as file handles and network connections as soon as possible.
- The using statement allows the programmer to specify when the resource object should be released. The object provided for the using statement must implement the IDisposable interface. This interface provides the Dispose method, which releases the resources of this object.
- You can exit the using statement when it reaches the end of the using statement or when an exception is thrown before the statement ends and the control leaves the statement block.
External alias
Sometimes it is necessary to reference two versions of an assembly with the same fully qualified type name, for example, when two or more versions of the Assembly need to be used in the same application. By using the external Assembly alias, the namespaces from each assembly can be packaged in the root-level namespace named by the alias, so that they can be used in the same file.
- To reference two assembly with the same fully qualified type name, you must specify the alias on the command line, as shown below:
/r:N1=a1.dll
/r:N2=a2.dll
- This will create an external alias
N1
AndN2
. To use these aliases from a program, use the extern keyword to reference them. For example:
extern alias N1;
extern alias N2;
Each external alias declaration introduces an additional root-level namespace, which is parallel to the global namespace, rather than within the global namespace. Therefore, the types from each assembly can be referenced by their own fully qualified names that are rooted in the appropriate namespace alias, without the ambiguity.
Example:
For example, there is an assembly a1.dll:
// Assembly a1.dll:
Namespace N
{
Public class {}
Public class B {}
}
Then reference the Assembly a2.dll:
Assembly a2.dll:
Namespace N
{
Public class B {}
Public class C {}
}
Then the following program will go wrong:
Class Test
{
N. A a; // OK
N. B B; // Error
N. C c; // OK
}
Then you can solve this problem by importing an external alias:
// First run csc/r: X = a1.dll/r: Y = a2.dll test. cs on the command line.
// Import an external alias
Extern alias X;
Extern alias Y;
Class Test
{
// Use: Use a real namespace from an alias
X: N. A;
X: N. B b1;
Y: N. B b2;
Y: N. C c;
}
For more information, see the MSDN document. |
All content in this Blog is provided as "the status quo" without any guarantee and no rights are granted. This posting is provided "as is" with no warranties, and confers no rights. |