C# 結構體 Struct
c# 結構體 struct
在 c# 中,結構體是值類型數據結構。它使得一個單一變量可以存儲各種數據類型的相關數據。struct 關鍵字用于創建結構體。
結構體是用來代表一個記錄。假設您想跟蹤圖書館中書的動態。您可能想跟蹤每本書的以下屬性:
- title
- author
- subject
- book id
1. 定義結構體
為了定義一個結構體,您必須使用 struct 語句。struct 語句為程序定義了一個帶有多個成員的新的數據類型。
例如,您可以按照如下的方式聲明 book 結構:
struct books { public string title; public string author; public string subject; public int book_id; };
下面的程序演示了結構的用法:
using system; using system.text; ? ? ? 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; ? ? ? ?/* 聲明 book1,類型為 books */ ? ? ? books book2; ? ? ? ?/* 聲明 book2,類型為 books */ ? ? ? /* book 1 詳述 */ ? ? ? book1.title = "c programming"; ? ? ? book1.author = "nuha ali"; ? ? ? book1.subject = "c programming tutorial"; ? ? ? book1.book_id = 6495407; ? ? ? /* book 2 詳述 */ ? ? ? book2.title = "telecom billing"; ? ? ? book2.author = "zara ali"; ? ? ? book2.subject = ?"telecom billing tutorial"; ? ? ? book2.book_id = 6495700; ? ? ? /* 打印 book1 信息 */ ? ? ? 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); ? ? ? /* 打印 book2 信息 */ ? ? ? 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 : nuha ali book 1 subject : c programming tutorial book 1 book_id : 6495407 book 2 title : telecom billing book 2 author : zara ali book 2 subject : telecom billing tutorial book 2 book_id : 6495700
2. c# 結構的特點
您已經用了一個簡單的名為 books 的結構。在 c# 中的結構與傳統的 c 或 c++ 中的結構不同。c# 中的結構有以下特點:
- 結構可帶有方法、字段、索引、屬性、運算符方法和事件。
- 結構可定義構造函數,但不能定義析構函數。但是,您不能為結構定義無參構造函數。無參構造函數(默認)是自動定義的,且不能被改變。
- 與類不同,結構不能繼承其他的結構或類。
- 結構不能作為其他結構或類的基礎結構。
- 結構可實現一個或多個接口。
- 結構成員不能指定為 abstract、virtual 或 protected。
- 當您使用 new 操作符創建一個結構對象時,會調用適當的構造函數來創建結構。與類不同,結構可以不使用 new 操作符即可被范例化。
- 如果不使用 new 操作符,只有在所有的字段都被初始化之后,字段才被賦值,對象才被使用。
3. 類 vs 結構
類和結構有以下幾個基本的不同點:
- 類是引用類型,結構是值類型。
- 結構不支持繼承。
- 結構不能聲明默認的構造函數。
針對上述討論,讓我們重寫前面的范例:
using system; using system.text; ? ? ? struct books { ? ?private string title; ? ?private string author; ? ?private string subject; ? ?private int book_id; ? ?public void setvalues(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(); /* 聲明 book1,類型為 books */ ? ? ? books book2 = new books(); /* 聲明 book2,類型為 books */ ? ? ? /* book 1 詳述 */ ? ? ? book1.setvalues("c programming", ? ? ? "nuha ali", "c programming tutorial",6495407); ? ? ? /* book 2 詳述 */ ? ? ? book2.setvalues("telecom billing", ? ? ? "zara ali", "telecom billing tutorial", 6495700); ? ? ? /* 打印 book1 信息 */ ? ? ? book1.display(); ? ? ? /* 打印 book2 信息 */ ? ? ? book2.display(); ? ? ? console.readkey(); ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果:
title : c programming author : nuha ali subject : c programming tutorial book_id : 6495407 title : telecom billing author : zara ali subject : telecom billing tutorial book_id : 6495700