命名空間的例子:

 namespace MyApplication
 {
     // The content to reside in the MyApplication namespace is placed here.
 }

命名空間的使用:

 System.Console.WriteLine("Hello, World!");

This will call the WriteLine method that is a member of the Console class within the System namespace.

using關鍵字將引入命名空間中的所有名字。

 using System;
 
 namespace MyApplication
 {
   class MyClass
   {
     void ShowGreeting()
     {
         Console.WriteLine("Hello, World!"); // note how System is now not required
     }
   }
 }

命名空間是全局的。兩個源文件中的同一個命名空間將被編譯器合併。

嵌套命名空間 編輯

 namespace CodeWorks
 {
     namespace MyApplication
     {
         // Do stuff
     }
 }

或者:

 namespace CodeWorks.MyApplication
 {
     // Do stuff
 }