C#中的struct

來源:互聯網
上載者:User

標籤:

平常在編程中很少使用到struct,今天在寫一個演算法題的過程中需要用到,於是到網上查了一下,發現struct裡面還是有很多東西我是不知道的,接下來就將我查的資料記錄下來與大家一起分享。

首先結構是實值型別。

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

struct 結構名
{
}

結構概述

結構具有以下特點:

  • 結構是實值型別,而類是參考型別。 (結構不能包含顯式的無參數建構函式)

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

  • 結構可以聲明建構函式,但它們必須帶參數。

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

  • 結構可以實現介面。

  • 結構在定義變數時不能給定初始值。(另加)

  • 如果要在結構中使用建構函式則必須給所有的變數賦值(在建構函式中直接給變數賦值而不是給變數的屬性賦值,因為在未賦值之前屬性是沒有值的所以不能直接給屬性)(另加)

  • 所有的結構都隱式繼承自ValueType類,不需要顯示指定;。(另加)

  • 結構的繼承列表中只允許有介面。(另加)

  • 結構的繼承是三層的:object >> valuetype >> "結構" 。(另加)

例如:

 1  struct Employeestruct 2     { 3         private string name; 4         public string Name 5         { 6             get { return name; } 7             set { name = value; } 8         } 9 10         private int age;11         public int Age12         {13             get { return age; }14             set { age = value; }15         }16 17         private int gongzhi;18         public int Gongzhi19         {20             get { return gongzhi; }21             //set { gongzhi = value; }22         }23         //有參數建構函式24         public Employeestruct(string _name, int _age, int _gongzhi)25         {26             //如果要在結構中使用建構函式則必須給所有的變數賦值(在建構函式中賦值)27             this.name = _name;28             this.age = _age;29             this.gongzhi = _gongzhi;30         }31     }32 33     //使用結構34 35     class Program36     {37         static void Main(string[] args)38         {39 40             ////執行個體化Employeestruct結構41             //Employeestruct empstruct = new Employeestruct("Steven", 22, 10000);42             //Console.WriteLine("姓名:{0}\n年齡:{1}\n工資:{2:C2}", empstruct.Name, empstruct.Age, empstruct.Gongzhi);43             Console.Read();44         }45     }

使用結構

struct 類型適於表示 Point、Rectangle 和 Color 等輕量對象。儘管可以將一個點表示為類,但在某些情況下,使用結構更有效。例如,如果聲明一個 1000 個 Point 對象組成的數組,為了引用每個對象,則需分配更多記憶體;這種情況下,使用結構可以節約資源。由於 .NET Framework 包含名為 Point 的對象,因此我們轉而調用結構“CoOrds”。

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

聲明結構的預設(無參數)建構函式是錯誤的。總是提供預設建構函式以將結構成員初始化為它們的預設值。在結構中初始化執行個體欄位也是錯誤的。

如果使用 new 運算子建立結構對象,則會建立該結構對象,並調用適當的建構函式。與類不同,結構的執行個體化可以不使用 new 運算子。如果不使用 new,則在初始化所有欄位之前,欄位都保持未賦值狀態且對象不可用。

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

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

除非需要參考型別語義,否則系統將較小的類作為結構處理效率會更高。

樣本1:


下面的樣本示範使用預設建構函式和參數化建構函式的 struct 初始化。
 1 public struct CoOrds 2 { 3     public int x, y; 4     public CoOrds(int p1, int p2) 5     { 6         x = p1; 7         y = p2; 8     } 9 }10 // Declare and initialize struct objects.11 class TestCoOrds12 {13     static void Main()14     {15         // Initialize:   16         CoOrds coords1 = new CoOrds();17         CoOrds coords2 = new CoOrds(10, 10);18 19         // Display results:20         System.Console.Write("CoOrds 1: ");21         System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);22 23         System.Console.Write("CoOrds 2: ");24         System.Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);25     }26 }

輸出

 

CoOrds 1: x = 0, y = 0

 

CoOrds 2: x = 10, y = 10

 

樣本2:

 


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

 1 public struct CoOrds 2 { 3     public int x, y; 4     public CoOrds(int p1, int p2) 5     { 6         x = p1; 7         y = p2; 8     } 9 }10 11 // Declare a struct object without "new."12 class TestCoOrdsNoNew13 {14     static void Main()15     {16         // Declare an object:17         CoOrds coords1;18 19         // Initialize:20         coords1.x = 10;21         coords1.y = 20;22 23         // Display results:24         System.Console.Write("CoOrds 1: ");25         System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);26     }27 }

輸出

CoOrds 1: x = 10, y = 20

 

 

 

C#中的struct

聯繫我們

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