使用結構(C# 編程指南)

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   for   

struct 類型適於表示 Point、Rectangle 和 Color 等輕量對象。 儘管使用自動實作屬性將一個點表示為類同樣方便,但在某些情況下使用結構更加有效。 例如,如果聲明一個 1000 個 Point 對象組成的數組,為了引用每個對象,則需分配更多記憶體;這種情況下,使用結構可以節約資源。 因為 .NET Framework 包含一個名為 Point 的對象,所以本樣本中的結構命名為“CoOrds”。

 1 public struct CoOrds 2 { 3     public int x, y; 4  5     public CoOrds(int p1, int p2) 6     { 7         x = p1; 8         y = p2; 9     }10 }

為結構定義預設(無參數)建構函式是錯誤的。 在結構體中初始化執行個體欄位也是錯誤的。 只能通過兩種方式初始化結構成員:一是使用參數化建構函式,二是在聲明結構後分別訪問成員。 對於任何私人成員或以其他方式設定為不可訪問的成員,只能在建構函式中進行初始化。

如果使用 new 運算子建立結構對象,則會建立該結構對象,並調用適當的建構函式。 與類不同,結構的執行個體化可以不使用 new 運算子。 在此情況下不存在建構函式調用,因而可以提高分配效率。 但是,在初始化所有欄位之前,欄位將保持未賦值狀態且對象不可用。

當結構包含參考型別作為成員時,必須顯式調用該成員的預設建構函式,否則該成員將保持未賦值狀態且該結構不可用。 (這將導致編譯器錯誤 CS0171。)

對於結構,不像類那樣存在繼承。 一個結構不能從另一個結構或類繼承,而且不能作為一個類的基。 但是,結構從基類 Object 繼承。 結構可實現介面,其方式同類完全一樣。

無法使用 struct 關鍵字聲明類。 在 C# 中,類與結構在語義上是不同的。 結構是實值型別,而類是參考型別。 有關更多資訊,請參見實值型別。

除非需要參考型別語義,將較小的類聲明為結構,可以提高系統的處理效率。

樣本 1 說明

下面的樣本示範使用預設建構函式和參數化建構函式的 struct 初始化。

代碼
 1 public struct CoOrds 2 { 3     public int x, y; 4  5     public CoOrds(int p1, int p2) 6     { 7         x = p1; 8         y = p2; 9     }10 }
 1 // Declare and initialize struct objects. 2 class TestCoOrds 3 { 4     static void Main() 5     { 6         // Initialize:    7         CoOrds coords1 = new CoOrds(); 8         CoOrds coords2 = new CoOrds(10, 10); 9 10         // Display results:11         Console.Write("CoOrds 1: ");12         Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);13 14         Console.Write("CoOrds 2: ");15         Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);16 17         // Keep the console window open in debug mode.18         Console.WriteLine("Press any key to exit.");19         Console.ReadKey();20     }21 }22 /* Output:23     CoOrds 1: x = 0, y = 024     CoOrds 2: x = 10, y = 1025 */
樣本 2 說明

下面舉例說明了結構特有的一種功能。 它在不使用 new 運算子的情況下建立 CoOrds 對象。 如果將 struct 換成 class,程式將不會編譯。

代碼
 1 public struct CoOrds 2 { 3     public int x, y; 4  5     public CoOrds(int p1, int p2) 6     { 7         x = p1; 8         y = p2; 9     }10 }
 1 // Declare a struct object without "new." 2 class TestCoOrdsNoNew 3 { 4     static void Main() 5     { 6         // Declare an object: 7         CoOrds coords1; 8  9         // Initialize:10         coords1.x = 10;11         coords1.y = 20;12 13         // Display results:14         Console.Write("CoOrds 1: ");15         Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);16 17         // Keep the console window open in debug mode.18         Console.WriteLine("Press any key to exit.");19         Console.ReadKey();20     }21 }22 // Output: CoOrds 1: x = 10, y = 20

轉子MSDN,詳見http://msdn.microsoft.com/zh-cn/library/0taef578(v=vs.100).aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.