跳轉到內容

C# 程式設計/資料結構

來自華夏公益教科書,開放的書籍,開放的世界

在 C# 中,有許多方法可以將資料集合組合在一起。

列舉 是一種資料型別,它透過為每個專案分配一個識別符號(名稱)來列舉一組專案,同時公開一個底層基本型別以對列舉元素進行排序。預設情況下,底層型別為 int,但可以是除了 char 之外的任何整數型別。

列舉的宣告如下

 enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

然後,上述列舉中的元素可作為常量使用

 Weekday day = Weekday.Monday;

 if (day == Weekday.Tuesday)
 {
     Console.WriteLine("Time sure flies by when you program in C#!");
 }

如果如上例所示未為列舉項分配顯式值,則第一個元素的值為 0,並且每個後續元素都被分配後續值。但是,可以將底層整數型別中的特定值分配給任何列舉元素(請注意,為了訪問基本型別,變數必須進行型別轉換)。

 enum Age { Infant = 0, Teenager = 13, Adult = 18 };
 
 Age myAge = Age.Teenager;
 Console.WriteLine("You become a teenager at an age of {0}.", (int)myAge);

當列舉的用途僅僅是將一組專案組合在一起時(例如,以比整數更有意義的方式表示國家、州或地理區域),列舉元素的底層值可能不會被使用。與其定義一組邏輯相關的常量,不如使用列舉更具可讀性。

可能需要建立一個基本型別不是 int 的列舉。為此,請指定除了 char 之外的任何整數型別,並使用基類擴充套件語法在列舉名稱之後,如下所示

 enum CardSuit : byte { Hearts, Diamonds, Spades, Clubs };

如果需要輸出值,列舉型別也很有用。透過呼叫列舉上的 .ToString() 方法,將輸出列舉名稱(例如,CardSuit.Hearts.ToString() 將輸出“Hearts”)。

結構體

[編輯 | 編輯原始碼]

結構體(關鍵字 struct)是輕量級物件。它們主要用於僅需要值型別變數集合的資料容器時。結構體類似,它們可以具有建構函式、方法,甚至可以實現介面,但存在重要的區別。

  • 結構體是值型別,而是引用型別,這意味著它們在作為引數傳遞給方法時表現不同。
  • 結構體不支援繼承。雖然結構體在使用上似乎有限制,但如果以正確的方式使用,它們需要更少的記憶體並且成本更低。
  • 結構體始終具有預設建構函式,即使您不想要一個。類允許您使用“private”修飾符隱藏建構函式,而結構體必須具有一個。

例如,可以這樣宣告一個 struct

 struct Person
 {
     public string name;
     public System.DateTime birthDate;
     public int heightInCm;
     public int weightInKg;
 }

然後,可以這樣使用 Person 結構體

 Person dana = new Person();
 dana.name = "Dana Developer";
 dana.birthDate = new DateTime(1974, 7, 18);
 dana.heightInCm = 178;
 dana.weightInKg = 50;
 
 if (dana.birthDate < DateTime.Now)
 {
     Console.WriteLine("Thank goodness! Dana Developer isn't from the future!");
 }

也可以為 struct 提供建構函式,以簡化初始化過程

 using System;
 struct Person
 {
     string name;
     DateTime birthDate;
     int heightInCm;
     int weightInKg;
 
     public Person(string name, DateTime birthDate, int heightInCm, int weightInKg)
     {
         this.name = name;
         this.birthDate = birthDate;
         this.heightInCm = heightInCm;
         this.weightInKg = weightInKg;
     }
 }
 
 public class StructWikiBookSample
 {
     public static void Main()
     {
         Person dana = new Person("Dana Developer", new DateTime(1974, 7, 18), 178, 50);
     }
 }

還有一種用於初始化結構體的替代語法

struct Person
{
    public string Name;
    public int Height;
    public string Occupation;
}

public class StructWikiBookSample2
{
    public static void Main()
    {
        Person john = new Person { Name = "John", Height = 182, Occupation = "Programmer" };
    }
}

結構體實際上僅出於效能原因使用,或者如果您打算按值引用它們。當結構體儲存的資料總量等於或小於 16 位元組時,結構體效果最佳。如果有疑問,請使用類。

陣列表示一組屬於相同型別的專案。宣告本身可以使用變數或常量來定義陣列的長度。但是,陣列具有固定長度,並且在聲明後無法更改。

// an array whose length is defined with a constant
int[] integers = new int[20];

int length = 0;
System.Console.Write("How long should the array be? ");
length = int.Parse(System.Console.ReadLine());
// an array whose length is defined with a variable
// this array still can't change length after declaration
double[] doubles = new double[length];
華夏公益教科書