A namespace is a domain in which all type names in a domain must be unique, with different types grouped into hierarchical namespaces.
The benefits of namespaces are: 1, avoid name collisions, 2, easy to find type names.
such as: System.secruity.Cryptogtaphy.
The following describes the use of the namespace keyword.
Namespace test
{
Class Class0
{
int i;
Public Class0 ()
{
}
}
}
The Using keyword uses
, access to the contents of a namespace can be through a fully qualified name. such as: Test.class0 to visit. But every time this is written in the program is very troublesome. You can use a using directive to reference a type.
The namespaces are all reference types. So at the beginning of the program you can refer to namespaces first. Just as you use VS to compile the software, every system-generated console has
using System;
Using System.Drawing;
Using System.Threading;
You can then use the type of namespace you are referencing. Note the use of the using, the following will be introduced.
Global namespaces
A global namespace consists of two parts:
1, all the top-level namespaces;
2, there is no type declared in any namespace;
For example, the test type is declared in the global namespace of the previous example, which is the first case. )
For example: Class outer{}//belongs to the second case;
Namespace test1{} is a global name.
Rules for namespaces
Name Scope
All names in the external namespaces that appear are implicitly introduced into the internal namespace. In this example middle and Class1 are implicitly introduced into the inner;
Namespace Outer
{
Namespace Middle
{
Class class1{}
namespace Inner
{
Class class2:class1{}
}
}
}
In this example: the following:
Namespace test{
Namespace common{
Class class1{}
}
Namespace managerreporting{
Class class2:common.class1{}
}
}
You can see that a test namespace contains two non-intersection namespaces. To refer to another, you must use a partially qualified name. Can you see the difference between him and the above example?
Name Masking
The name of the internal namespace masks the same name as the outer space.
For example, namespace outer{
Namespace middle{
Class class1{}
Class class2{}
}
Namespace inner{
Class class1{
Class2 Cinner;
Middle.class2 Couter;
}
Class class2{}
}
}
In this example. Two namespaces are declared in the outer namespace, and in the inner namespace, cinner refers to the type of inner.class2. Couter refers to the Middle.class2 type. A curly brace indicates that the temporary variable inside is released when the program finishes running. The masking function simply refers to using a simple name to refer to a type that has an effect. If you want to reference the Class2 type of the middle namespace. You can cite couter like a village. Use a partially qualified name or a fully qualified name. You must know that masking has no effect on runtime semantics. (because the compiler always converts a name to a fully qualified name when parsing a namespace rule.) The Il language generated after compilation does not include unqualified or partially qualified names.
repeating namespaces
You can declare a namespace repeatedly, as long as the type name of the namespace does not conflict;
Namespace outer.middle.inner{
Class class1{}
}
Namespace outer.middle.inner{
Class class2{}
}
The individual thinks you can think of overloading the function.
You can even divide this example into two source files so that you can compile each class into a different assembly code;
SOURCE File # #
Namespace outer.middle.inner{
Class class1{}}
SOURCE File # #
Namespace outer.middle.inner{
Class class2{}
}
Nested instruction using
A using directive can be nested within a namespace. This allows the scope of the using directive to be scoped to the declaration of the namespace.
Namespace n1{
Class class1{}
}
Namespace n2{
Using N1;
Class class2:class1{}
}//compile true;
Namespace n3{
Class class3:class1{}
}//compile Error
Note that this kind of writing, in VS is not passed, remember the previous said? Do you have to add a partial or fully qualified name in order to reference a type in a hierarchy?
Type aliases and namespaces
Introducing namespaces can cause type name collisions, rather than introducing all of the namespaces, to select only the type of attraction you want to use, giving each alias that you want to use.
Using P=system. Reflection
Class Program{p F;}
But we have absolutely no need to do so. Because we can turn them into global references. In the village you open vs, the console is generated the same.
Advanced namespace Attributes
Extern
The external namespace allows the same name to appear. It can be as long as it contains a different type. This situation is most likely to occur in different files.
Gallery #:
Namespace widgets{
public class widget{}
}
Library #:
Namespace widgets{
public class widget{}
}
Program:
Using Widgets;
Namespace test{
static void Main ()
{
Widget W=new widget ();
}//compile Errror;
}
This procedure is incorrect because the widget is not deterministic.
The solution to this problem is to refer to the fully qualified name.
Csc/r:w1=widgets1.dll/rw2=widgets2.dll Application.cs
or use the extern alias W1;extern alias W2;
Namespace alias qualification
As mentioned earlier, the name of the inner namespace will mask the name of the outer namespace. However. Sometimes even using fully qualified types does not resolve this conflict. As follows:
Namespace n{
Class a{
public class b{}
static void Main ()
{
New A.B ();
}
}
}
Namespace a{
Class b{}
}
This is the nested b that is running. Note that the same namespace and type cannot be included in the same file in vs. But in different files may appear.
There are two ways to resolve a possible namespace conflict problem:
1, using the above mentioned using aliases.
2. The global namespace----the root of all namespaces.
Namespace test{
static void Main ()
{
System. Console.WriteLine (New A.b ());
System.Console.WriteLine (New global::a.b ());
}
}
Alias Qualification:
External alias W1;
External alias W2;
Namespace test{
static void Main ()
{
W1. Widgets.widget w1=new W1. Widgets.widget ();
W2. Widgets.widget w2=new W2. Widgets.widget ();
}
}
The content of namespace is finished, please correct me if there is any mistake.
C # Namespace Detailed namespace