Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

จะกำหนดเนมสเปซใน C # ได้อย่างไร?


เนมสเปซได้รับการออกแบบมาเพื่อจัดเตรียมวิธีแยกชื่อชุดหนึ่งออกจากชุดอื่น นิยามเนมสเปซเริ่มต้นด้วยเนมสเปซคำหลักตามด้วยชื่อเนมสเปซดังนี้ −

namespace namespace_name {
// code declarations
}

ต่อไปนี้เป็นตัวอย่างที่แสดงวิธีการใช้เนมสเปซใน C# -

ตัวอย่าง

using System;

namespace first_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside first_space");
      }
   }
}
namespace second_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside second_space");
      }
   }
}
class TestClass {
   static void Main(string[] args) {
      first_space.namespace_cl fc = new first_space.namespace_cl();
      second_space.namespace_cl sc = new second_space.namespace_cl();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

ผลลัพธ์

Inside first_space
Inside second_space