中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 結(jié)構(gòu)體(Struct)

在 C# 中,結(jié)構(gòu)體是值類型數(shù)據(jù)結(jié)構(gòu)。它使得一個單一變量可以存儲各種數(shù)據(jù)類型的相關(guān)數(shù)據(jù)。struct 關(guān)鍵字用于創(chuàng)建結(jié)構(gòu)體。

結(jié)構(gòu)體是用來代表一個記錄。假設(shè)您想跟蹤圖書館中書的動態(tài)。您可能想跟蹤每本書的以下屬性:

  • Title
  • Author
  • Subject
  • Book ID

定義結(jié)構(gòu)體

為了定義一個結(jié)構(gòu)體,您必須使用 struct 語句。struct 語句為程序定義了一個帶有多個成員的新的數(shù)據(jù)類型。

例如,您可以按照如下的方式聲明 Book 結(jié)構(gòu):

struct Books
{
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

下面的程序演示了結(jié)構(gòu)的用法:

實例

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,類型為 Book */
? ? ? Books Book2; ? ? ? ?/* 聲明 Book2,類型為 Book */

? ? ? /* 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();

? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

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

C# 結(jié)構(gòu)的特點

您已經(jīng)用了一個簡單的名為 Books 的結(jié)構(gòu)。在 C# 中的結(jié)構(gòu)與傳統(tǒng)的 C 或 C++ 中的結(jié)構(gòu)不同。C# 中的結(jié)構(gòu)有以下特點:

  • 結(jié)構(gòu)可帶有方法、字段、索引、屬性、運算符方法和事件。
  • 結(jié)構(gòu)可定義構(gòu)造函數(shù),但不能定義析構(gòu)函數(shù)。但是,您不能為結(jié)構(gòu)定義無參構(gòu)造函數(shù)。無參構(gòu)造函數(shù)(默認)是自動定義的,且不能被改變。
  • 與類不同,結(jié)構(gòu)不能繼承其他的結(jié)構(gòu)或類。
  • 結(jié)構(gòu)不能作為其他結(jié)構(gòu)或類的基礎(chǔ)結(jié)構(gòu)。
  • 結(jié)構(gòu)可實現(xiàn)一個或多個接口。
  • 結(jié)構(gòu)成員不能指定為 abstract、virtual 或 protected。
  • 當(dāng)您使用 New 操作符創(chuàng)建一個結(jié)構(gòu)對象時,會調(diào)用適當(dāng)?shù)臉?gòu)造函數(shù)來創(chuàng)建結(jié)構(gòu)。與類不同,結(jié)構(gòu)可以不使用 New 操作符即可被實例化。
  • 如果不使用 New 操作符,只有在所有的字段都被初始化之后,字段才被賦值,對象才被使用。

類 vs 結(jié)構(gòu)

類和結(jié)構(gòu)有以下幾個基本的不同點:

  • 類是引用類型,結(jié)構(gòu)是值類型。
  • 結(jié)構(gòu)不支持繼承。
  • 結(jié)構(gòu)不能聲明默認的構(gòu)造函數(shù)。

針對上述討論,讓我們重寫前面的實例:

實例

using System;
using System.Text;
? ? ?
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(); /* 聲明 Book1,類型為 Book */
? ? ? Books Book2 = new Books(); /* 聲明 Book2,類型為 Book */

? ? ? /* book 1 詳述 */
? ? ? Book1.getValues("C Programming",
? ? ? "Nuha Ali", "C Programming Tutorial",6495407);

? ? ? /* book 2 詳述 */
? ? ? Book2.getValues("Telecom Billing",
? ? ? "Zara Ali", "Telecom Billing Tutorial", 6495700);

? ? ? /* 打印 Book1 信息 */
? ? ? Book1.display();

? ? ? /* 打印 Book2 信息 */
? ? ? Book2.display();

? ? ? Console.ReadKey();

? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

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