在 C# 中,結構體是一種值型別資料型別。它可以幫助您使單個變數儲存各種資料型別的相關資料在一起。struct
關鍵字用於定義和建立一個結構。
結構體用於表示記錄資訊。假設您想在圖書館中跟蹤記錄書籍資訊。假設希望跟蹤每本書的以下屬性:
要定義一個結構,需要使用struct
語句。struct
語句定義了一個新的資料型別,有多個程式的成員。
例如,以下是宣告Book
結構體的方式:
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
以下範例程式顯示如何使用結構體:
using System;
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1; /* Declare Book1 of type Book */
Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
Book1.title = "C Programming";
Book1.author = "Maxsu";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 5493427;
/* book 2 specification */
Book2.title = "Telecom Billing";
Book2.author = "Sukida";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 8493480;
/* print Book1 info */
Console.WriteLine("Book 1 title : {0}", Book1.title);
Console.WriteLine("Book 1 author : {0}", Book1.author);
Console.WriteLine("Book 1 subject : {0}", Book1.subject);
Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);
/* print Book2 info */
Console.WriteLine("Book 2 title : {0}", Book2.title);
Console.WriteLine("Book 2 author : {0}", Book2.author);
Console.WriteLine("Book 2 subject : {0}", Book2.subject);
Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);
Console.ReadKey();
}
}
當編譯和執行上述程式碼時,會產生以下結果:
Book 1 title : C Programming
Book 1 author : Maxsu
Book 1 subject : C Programming Tutorial
Book 1 book_id :5493427
Book 2 title : Telecom Billing
Book 2 author : Sukida
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 8493480
在上面地範例中,我們已經使用了一個名為Books
的簡單結構體。 C# 中的結構與傳統的C語言或C++中的結構完全不同。 C# 結構具有以下特點:
New
運算子建立一個struct
物件時,它將呼叫相應的建構函式。 與類不同,可以在不使用New
運算子的情況下範例化結構體。New
運算子,則欄位保持未分配,並且在所有欄位初始化之前不能使用物件。類和結構有以下基本差別:
根據上述討論,下面我們來重寫上一個例子:
using System;
struct Books
{
private string title;
private string author;
private string subject;
private int book_id;
public void getValues(string t, string a, string s, int id)
{
title = t;
author = a;
subject = s;
book_id = id;
}
public void display()
{
Console.WriteLine("Title : {0}", title);
Console.WriteLine("Author : {0}", author);
Console.WriteLine("Subject : {0}", subject);
Console.WriteLine("Book_id :{0}", book_id);
}
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1 = new Books(); /* Declare Book1 of type Book */
Books Book2 = new Books(); /* Declare Book2 of type Book */
/* book 1 specification */
Book1.getValues("C Programming",
"Maxsu", "C Programming Tutorial", 749540712);
/* book 2 specification */
Book2.getValues("Telecom Billing",
"Sukida", "Telecom Billing Tutorial", 59570011);
/* print Book1 info */
Book1.display();
/* print Book2 info */
Book2.display();
Console.ReadKey();
}
}
當編譯和執行上述程式碼時,會產生以下結果:
Title : C Programming
Author : Maxsu
Subject : C Programming Tutorial
Book_id :749540712
Title : Telecom Billing
Author : Sukida
Subject : Telecom Billing Tutorial
Book_id :59570011