C# study notes namespace

Namespaces

1. The concept of namespace

Namespace (namespace) is a way of hierarchical planning of various types of names. Namespace actually provides a naming mechanism and is also a way of logical organization of programs.
A namespace is a loose collection of some types. Generally, classes in the same namespace are not required to have a clear interrelationship, such as containment, inheritance, and so on. In order to facilitate programming and management, the types that need to work together are usually placed in a namespace. The various classes and interfaces the System namespace, including System.Console, , System.String, System.Random, System.Math, System.GC, System.IDisposableetc., indicate that these types of systems is directly related to the core, the base language. Namespace is a hierarchical organization, such as System, System.IO, System.IO.IsolatedStorageis a three-level namespace.

The use of namespaces does not indicate accessibility, that is internal, it protectedis not directly related to and. The name space also does not indicate the physical combination of the target program. A program can have classes from various namespaces, or multiple namespaces can be defined. The name space also does not indicate the storage method of the source file, that is, it is not necessary to place the definitions of the classes in the same name space in the same directory, but it is best.

2. Namespace declaration

Use keywords when declaring a namespace namespace, the declaration method is as follows:

namespace 名字
{
    
    
	...
}

The name of the namespace can be an identifier, or multiple identifiers separated by dots (.), such as System.IO. The main namespace declaration is included content: each type declaration ( struct, , enum, class, interface) delegateand nested namespace declaration. E.g:

namespace N1				// N1
{
    
    
	class C1				// N1.C1
	{
    
    
		class C2			// N1.C1.C2
		{
    
    
		}
	}
	namespace N2			// N1.N2
	{
    
    
		class C2			// N1.N2.C2
		{
    
    
		}
	}
}

Using multiple dots to write names and nesting methods have the same meaning. E.g:

namespace N1.N2
{
    
    
	class A{
    
    }
	class B{
    
    }
}

Semantically, it is the same as the following code

namespace N1
{
    
    
	namespace N2
	{
    
    
		class A{
    
    }
		class B{
    
    }
	}
}

The namespace is open, which means that namespaces can be merged. The two classes in the above namespace can be defined separately:

namespace N1.N2
{
    
    
	class A{
    
    }
}
namespace N1.N2
{
    
    
	class B{
    
    }
}

The accessibility of the namespace is implicit public, but it cannot be explicitly modified with any modifiers.
When a namespace declaration is the highest-level declaration in the compilation unit (source file), it becomes part of the global space. If a type declaration is not in the namespace, the type declaration belongs to the global namespace.

3. Import of namespace

Import the namespace, use the keyword using, and its format is as follows:

using 命名空间;

Note: using can also mean importing a staticclass, such as using static System.Console;.

4. Use aliases

After importing the namespace, problems may occur with the same name, as using N1and using N2later, in namespace N1 and N2 are present in class C, then C will only write the ambiguity, the solution is to use the full name, such as N1.Cand N2.C.
Another way is to use namespace or class aliases. E.g:

using C1 = N1.C;
using C2 = N2.C;

In this way, new N1.Cit can be written as new C1().

using 别名 = 命名空间或类名;

The alias indicator can be placed at the top of a program or in the namespace, but it must be placed in front of the type declaration and the nested namespace. E.g:

namespace N1.N2
{
    
    
	class A {
    
    }
}
namespace N3
{
    
    
	using A = N1.N2.A;
	class B : A {
    
    }
}

The use of aliases is not only conducive to conflict resolution, but also can be used to simplify writing, for example:

using CodeIds = System.Xml.Serialization.CodeIdentifiers;

Guess you like

Origin blog.csdn.net/qq_45349225/article/details/114139659