結構(C# ) (摘抄msdn.備忘.)

來源:互聯網
上載者:User
文章目錄
  • 結構(C# 編程指南)
  • 使用結構(C# 編程指南)
結構(C# 編程指南)

Visual Studio 2010

其他版本

  • Visual Studio 2008
  • Visual Studio 2005

0(共 1)對本文的評價是有協助 評價此主題

結構是使用 struct 關鍵字定義的,例如:

C#

複製

public struct PostalAddress{    // Fields, properties, methods and events go here...}

結構與類共用大多數相同的文法,但結構比類受到的限制更多:

  • 在結構聲明中,除非欄位被聲明為 const 或 static,否則無法初始化。

  • 結構不能聲明預設建構函式(沒有參數的建構函式)或解構函式。

  • 結構在賦值時進行複製。將結構賦值給新變數時,將複製所有資料,並且對新副本所做的任何修改不會更改原始副本的資料。在使用實值型別的集合(如 Dictionary<string, myStruct>)時,請務必記住這一點。

  • 結構是實值型別,而類是參考型別。

  • 與類不同,結構的執行個體化可以不使用 new 運算子。

  • 結構可以聲明帶參數的建構函式。

  • 一個結構不能從另一個結構或類繼承,而且不能作為一個類的基。所有結構都直接繼承自 System.ValueType,後者繼承自 System.Object。

  • 結構可以實現介面。

  • 結構可用作可為 Null 的型別,因而可向其賦 null 值。

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

Visual Studio 2010

其他版本

  • Visual Studio 2008
  • Visual Studio 2005

2(共 3)對本文的評價是有協助 評價此主題

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

C#

複製

public struct CoOrds{    public int x, y;    public CoOrds(int p1, int p2)    {        x = p1;        y = p2;    }}

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

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

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

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

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

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

樣本 1

說明

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

代碼

C#

複製

publicstruct CoOrds{    publicint x, y;    public CoOrds(int p1, int p2)    {        x = p1;        y = p2;    }}

C#

複製

// Declare and initialize struct objects.class TestCoOrds{    staticvoid Main()    {        // Initialize:           CoOrds coords1 = new CoOrds();        CoOrds coords2 = new CoOrds(10, 10);        // Display results:        Console.Write("CoOrds 1: ");        Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);        Console.Write("CoOrds 2: ");        Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);        // Keep the console window open in debug mode.        Console.WriteLine("Press any key to exit.");        Console.ReadKey();    }}/* Output:    CoOrds 1: x = 0, y = 0    CoOrds 2: x = 10, y = 10*/

樣本 2

說明

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

代碼

C#

複製

publicstruct CoOrds{    publicint x, y;    public CoOrds(int p1, int p2)    {        x = p1;        y = p2;    }}

C#

複製

// Declare a struct object without "new."class TestCoOrdsNoNew{    staticvoid Main()    {        // Declare an object:        CoOrds coords1;        // Initialize:        coords1.x = 10;        coords1.y = 20;        // Display results:        Console.Write("CoOrds 1: ");        Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);        // Keep the console window open in debug mode.        Console.WriteLine("Press any key to exit.");        Console.ReadKey();    }}// Output: CoOrds 1: x = 10, y = 20

請參見

參考

類和結構(C# 編程指南)

結構(C# 編程指南)

概念

C# 編程指南

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.