C# 中的命名空間

WBOY
發布: 2024-09-03 15:03:00
原創
779 人瀏覽過

在 C# 中,命名空間用於組織許多類,以便非常輕鬆地處理應用程式。這有助於將一組名稱與另一組名稱分開。一個命名空間中聲明的類別名稱不能與另一個命名空間中聲明的相同類別名稱發生衝突。它允許使用群組中的分層系統系統地組織代碼。分層系統可用於定義嵌套名稱空間。您可以透過將程式碼放置在不同的命名空間中來使程式碼保持井井有條。

命名空間是一個描述性區域,其中標識符(類型名稱、功能、變數等)被賦予一個範圍。命名空間用於將程式碼排列成邏輯組並防止名稱衝突,特別是當您的程式碼庫中包含各種庫時。

在 C# 中,根命名空間被視為全域命名空間。 global::System 定義了.Net Framework 的命名空間「System」。 System.console 可以在 C# 程式中使用。 System可以被定義為命名空間,而Console可以被視為一個類別。預設情況下,.NET Framework 提供了大量的命名空間來輕鬆實作應用程式。

命名空間概述

命名空間有一些屬性如下:

  • 命名空間用於組織更大的程式碼項目。
  • 命名空間使用點 (.) 運算子分隔。
  • 在C#中,類別的全名以其命名空間名稱開頭,後面跟著點運算子和類別名稱,稱為類別的完全限定名稱。
  • 指令「using」消除了為每個類別指定命名空間名稱的要求。
  • 在一個命名空間中,宣告的類別名稱不會與另一命名空間中宣告的相同類別名稱衝突。
  • 全域命名空間是根命名空間,global::System指的是.NET系統命名空間。

為什麼 C# 需要命名空間?

  • C# 程式中的命名空間有助於組織您的程式。這些命名空間也有助於避免兩組程式碼中的名稱類別之間發生衝突。
  • 如果您想重複使用某些程式碼,那麼在您自己的程式碼中實作命名空間是一個很好的做法。檔案或目錄名稱與命名空間不對應。如果這些與命名空間相對應,您可以這樣做來組織程式碼。
  • 命名空間在編寫更簡潔的程式碼和管理更大的專案方面發揮著至關重要的作用。
  • 在c#中使用命名空間的主要目的是減少.NET應用程式中的程式碼冗餘。簡而言之,命名空間是一組類,而類別是物件和方法的集合。您只需使用命名空間並在應用程式中匯入命名空間即可輕鬆存取所有類別方法。擁有命名空間使您能夠管理類別的範圍和方法。如果沒有命名空間,則無法使用多個同名的類別。

如何在 C# 定義命名空間?

命名空間可以透過使用關鍵字namespace後接namespace_name來定義。以下語法描述如何在 C# 程式語言中定義命名空間:

namespace namespace_name {
// body of namespace
}
登入後複製

範例:

namespace my_program_demo
{
class demo
{
public void myfunction()
{
// your code declarations
}
}
}
登入後複製

在上面的程式碼片段中,my_program_demo是一個命名空間,它包含一個類別demo作為其成員,myfunction()是demo類別的一個方法。

如何在 C# 中存取命名空間?

命名空間的類別可以透過使用關鍵字來訪問,該關鍵字指定程式在給定命名空間中使用的名稱。此關鍵字提供對要在 .NET 應用程式中使用的相關類別和方法的存取。 using 關鍵字允許使用類別而無需定義命名空間。

在c#中,您也可以使用點運算子存取命名空間的成員。 (命名空間_名稱.成員_名稱)

範例:

using System;
namespace Demo {
class DemoExample {
static void Main(string[] args) {
Console.WriteLine("Welcome to C# namespace...");
Console.ReadKey();
}
}
}
登入後複製

執行程序的步驟:

  • 使用命令列而不是Visual Studio IDE編譯C#程序,如下所示:
  • 開啟文字編輯器,包含上述程式碼並將檔案儲存為 DemoExample.cs
  • 開啟命令提示字元並進入檔案保存的目錄。
  • 輸入 csc DemoExample.cs 並按 Enter 編譯程式碼。
  • 進入該目錄,您將看到DemoExample.exe可執行檔。
  • 輸入 DemoExample 來執行程序,輸出將顯示在螢幕上。

範例:

using 關鍵字: 下面的範例指定了 using 關鍵字的用法。

using System;
using first_demo;
using second_demo;
namespace first_demo {
class myclass {
public void func1() {
Console.WriteLine("Helloworld.....");
}
}
}
namespace second_demo {
class myclass1 {
public void func2() {
Console.WriteLine("Welcome to EDUCBA.....");
}
}
}
class DemoClass {
static void Main(string[] args) {
myclass cls = new myclass();
myclass1 cls1 = new myclass1();
cls.func1();
cls1.func2();
Console.ReadKey();
}
}
登入後複製

編譯並執行上面的程式碼,你將得到如下圖所示的結果。

巢狀命名空間:建立巢狀命名空間的語法如下

namespace NamespaceDemo
{
namespace NestedNamespace
{
// code for nested namespace
}
}
登入後複製

The below example shows usage of nested namespaces: The members of a nested namespace can be accessed by using dot (.) operator:

using System;
using first_demo;
using first_demo.second_demo;
namespace first_demo {
class myclass {
public void func1() {
Console.WriteLine("Helloworld.....");
}
}
namespace second_demo {
class myclass1 {
public void func2() {
Console.WriteLine("This is example of nested namespace.....");
}
}
}
}
class NestedNamespaceDemo {
static void Main(string[] args) {
myclass cls = new myclass();
myclass1 cls1 = new myclass1();
cls.func1();
cls1.func2();
Console.ReadKey();
}
}
登入後複製

Compile and execute the above code, and you will get the result as shown in the image below:

Conclusion

  • Using namespaces, c# programs are structured and use the directives to promote the use of namespaces. From this document, we can comprehend the need and use of namespaces in classes. Namespaces can also contain other types as their members, such as classes, interfaces, structures, enumerations, and delegates.
  • Namespaces are used as both an inner organizational system for a program and as an external organizational system to present program aspects exposed to other programs. It regulates the scope of methods and classes in bigger projects. Net programming. Namespaces are also used for solving the naming conflict problem.

以上是C# 中的命名空間的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!