First, namespace nesting
The previous section describes namespaces that prevent class conflicts during object instantiation of a class, if two class names are contained in two different namespaces, Then the system prompts you to choose which class to use when the instance object is created. A namespace nesting is defined as a namespace nesting that can also contain another namespace nesting. Here's a simple example to illustrate:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Collections.ObjectModel;
Using System.Collections;
Using System.Reflection;
Namespace csharp//the outermost namespace
{
Classprogram
{
Staticvoidmain (string[] args) {
Csharp.man Mike = new Csharp.man ()//Create man in namespace Csharp
Counter.monkey Wukong = Newglobal::csharp.counter.monkey ();
Create the. Monkey class object in the namespace counter
Counter.Nestedspace.Man Jakson = new Counter.Nestedspace.Man ();
To create the man class object in the namespace counter, you must distinguish two mans from the above method
Console.Write ("Csharp.man ' name Is{0}", Mike.name);
Console.WriteLine ("Counter.monkey ' s name is {0}", wukong.name);
Console.WriteLine ("Counter.Nestedspace.Man ' name Is{0}", Jakson.name);
Output the names of three objects
Console.readkey ();
}
}
Namespace Csharp
{
Classman
{
Publicstring name;
Public Mans ()
{
THIS.name = "name in Mans";
}
}
}
Namespace Counter
{
Classmonkey
{
Publicstring name;
Public Monkey ()
{
THIS.name = "name in monkey";
}
}//namespace Nestedspace nested inside namespace counter
Namespace Nestedspace
{
Classman
{
Publicstring name;
Public Mans ()
{
THIS.name = "nested name";
}}}}}
}
Results of output:
Two, same namespace two class method of the same name
Now add the following method to the two man classes, and look at the result of their object calling this method
public void ChangeName (string newName)
{this.name = NewName;}
Mike.changename ("Andy Lau"); Jakson.changename ("Mike Jachson")
Three Alias namespaces
At the beginning of the program reference:
Using NewName = Csharp.csharp;
Alias, original name.
Let's look at a simple example of adding code to the above program:
Using NewName = csharp.csharp;//Reference
Console.WriteLine ("Using NewName = Csharp.csharp;");
Newname.man NewMan = new Newname.man ()//also create Mana object
Console.WriteLine ("NewMan ' name Is__{0}", Newman.name);
Newman.changename ("Guo Jing");
Console.WriteLine ("NewMan ' name Is__{0}", Newman.name);
Results of program operation: