1. 程式人生 > >C# 中的結構型別(struct)

C# 中的結構型別(struct)

有時候,類中只包含極少的資料,因為管理堆而造成的開銷顯得極不合算。這種情況下,更好的做法是使用結構(struct)型別。由於 struct 是值型別,是在棧(stack)上儲存的,所以能有效的減少記憶體管理的開銷(當然前提是這個結構足夠小)。
結構可以包含它自己的欄位、方法和構造器。
int 實際上是 Sysytem.Int32 結構型別。


編譯器始終會生成一個預設的構造器,若自己寫預設構造器則會出錯(預設構造器始終存在)。自己只能寫非預設構造器,並且在自己寫的構造器中初始化所有欄位。

  1. struct Time  
  2. {  
  3.     public Time()  
  4.     {   
  5.        // 編譯時錯誤:Structs cannot contain explicit parameterless constructors
  6.     }  
  7. }  
  8. struct NewYorkTime  
  9. {  
  10.     privateint hours, minutes, seconds;  
  11.     public NewYorkTime(int hh, int mm)  
  12.     {  
  13.         hours = hh;  
  14.         minutes = mm;  
  15.     }   // 編譯時錯誤,因為 seconds 未初始化
  16. }  

        可以使用 ? 修飾符建立一個結構變數的可空(nullable)的版本。然後把 null 值賦給這個變數。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace structType  
  6. {  
  7.     class Program  
  8.     {  
  9.         staticvoid Main(string[] args)  
  10.         {  
  11.             NewYorkTime? currentTime = null;    // 結構型別也是值型別,可以宣告為可空
  12.         }  
  13.     }  
  14.     struct NewYorkTime  
  15.     {  
  16.         privateint hours, minutes, seconds;  
  17.         public NewYorkTime(int hh, int mm)  
  18.         {  
  19.             hours = hh;  
  20.             minutes = mm;  
  21.             seconds = 0;  
  22.         }  
  23.     }  
  24. }  
        預設構造器不需要也不能自己定義,預設構造器會把所有的自動初始化為 0 。
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace structType  
  6. {  
  7.     class Program  
  8.     {  
  9.         staticvoid Main(string[] args)  
  10.         {  
  11.             Time now = new Time();  // 呼叫預設構造器,從而自動初始化,所有欄位為 0
  12.         }  
  13.     }  
  14.     struct Time  
  15.     {  
  16.         privateint hours, minutes, seconds;  
  17.     }  
  18. }  

        欄位(field)值如下:


        下面這種方式,結構將不會被初始化,但是也不能訪問。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace structType  
  6. {  
  7.     class Program  
  8.     {  
  9.         staticvoid Main(string[] args)  
  10.         {  
  11.             Time now;  // 不進行初始化,若訪問欄位的值會造成編譯錯誤
  12.         }  
  13.     }  
  14.     struct Time  
  15.     {  
  16.         privateint hours, minutes, seconds;  
  17.     }  
  18. }  
        欄位(field)值如下

        自己定義的構造器必須在構造器內把所有的欄位初始化。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace structType  
  6. {  
  7.     class Program  
  8.     {  
  9.         staticvoid Main(string[] args)  
  10.         {  
  11.             Time now = new Time(12, 30);  
  12.         }  
  13.     }  
  14.     struct Time  
  15.     {  
  16.         privateint hours, minutes, seconds;  
  17.         public Time(int hh, int mm)  
  18.         {  
  19.             hours = hh;  
  20.             minutes = mm;  
  21.             seconds = 0;  
  22.         }  
  23.     }  
  24. }  
        欄位(field)值如下

        結構中的欄位不能在宣告的同時進行初始化。

  1. struct Time  
  2. {  
  3.     privateint hours = 0;  // 報錯 'Time.hours': cannot have 
  4.                             // instance field initializers in structs
  5.     privateint minutes, seconds;  
  6.     public Time(int hh, int mm)  
  7.     {  
  8.         hours = hh;  
  9.         minutes = mm;  
  10.         seconds = 0;  
  11.     }  
  12. }