/**/
/*
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 9:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
... {
class Test
...{
static void Main()
...{
A.PrintName a = new A.PrintName();
a.intro();
B.PrintName b = new B.PrintName();
b.intro();
//通过以上方法实现在不同的命名空间里面的调用
}
}
}
namespace A
... {
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A");
}
}
}
namespace B
... {
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is B");
}
}
}
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 9:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
... {
class Test
...{
static void Main()
...{
A.PrintName a = new A.PrintName();
a.intro();
B.PrintName b = new B.PrintName();
b.intro();
//通过以上方法实现在不同的命名空间里面的调用
}
}
}
namespace A
... {
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A");
}
}
}
namespace B
... {
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is B");
}
}
}
使用命名空间的好处:
代码可以在多个文件中,命名空间具有扩展性,堆砌的层次式的类组织结构.
下面是命名空间可以嵌套的例子:
using
System;
namespace CG
... {
class Test
...{
static void Main()
...{
A.A1.PrintName a1 = new A.A1.PrintName();
a1.intro();
A.A2.PrintName a2 = new A.A2.PrintName();
a2.intro();
//通过以上方法实现在不同的命名空间里面的调用
}
}
}
namespace A
... {
namespace A1
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A1");
}
}
}
namespace A2
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A2");
}
}
}
}
namespace CG
... {
class Test
...{
static void Main()
...{
A.A1.PrintName a1 = new A.A1.PrintName();
a1.intro();
A.A2.PrintName a2 = new A.A2.PrintName();
a2.intro();
//通过以上方法实现在不同的命名空间里面的调用
}
}
}
namespace A
... {
namespace A1
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A1");
}
}
}
namespace A2
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A2");
}
}
}
}
在同一命名空间但在不同的文件中使用方法
#region 在同一命名空间但在不同的文件中使用方法
/**//*
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 9:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
...{
class Test
...{
static void Main()
...{
PrintName a1 = new PrintName();
a1.intro();
}
}
}
#endregion
PrintName另外一个文件里面的内容 #region PrintName另外一个文件里面的内容
/**//*
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 10:21
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A1");
}
}
}
#endregion
编译时可用csc namespace .cs PrintName.cs可以实现,运行时直接namespace.exe就可以显示正确结果
/**//*
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 9:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
...{
class Test
...{
static void Main()
...{
PrintName a1 = new PrintName();
a1.intro();
}
}
}
#endregion
PrintName另外一个文件里面的内容 #region PrintName另外一个文件里面的内容
/**//*
* Created by SharpDevelop.
* User: 陈庆伟
* Date: 2008-1-17
* Time: 10:21
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace CG
...{
public class PrintName
...{
public void intro()
...{
Console.WriteLine("My name is A1");
}
}
}
#endregion
编译时可用csc namespace .cs PrintName.cs可以实现,运行时直接namespace.exe就可以显示正确结果
由上可以看出相同的命名空间可以分布在不同的文件中
为了方便人们通常把printname.cs文件编译成.dll文件,编译方法如下:
在cmd下输入:csc /target:library PrintName.cs
就可以发现编译了一个PrintName.dll文件
在cmd下输入:csc /reference:PrintName.dll namespace.cs
在cmd下输入:namespace就可以得到正确的结果.
别名(Alias)
用简短的名词代替欲使用的类
using a = parentNamespace.childNamespace