C# 結構與類別

WBOY
發布: 2024-09-03 15:07:59
原創
718 人瀏覽過

顧名思義,C# 使用 struct 關鍵字來定義值類型,而 Class 使用 class 關鍵字來定義參考類型。在C#中,結構體中定義的變數儲存在堆疊中或以給定的變數類型存儲,其實例稱為結構體變數。然而,對於“類別”,實例被稱為物件並儲存在堆結構記憶體單元中。就建構子和析構函數而言,C# struct 不能有析構函數,但 Class 可以有析構函數。 Class 中允許進行成員分類,例如抽象類型、虛擬類型和受保護類型。

C# 結構與類別之間的頭對頭比較(資訊圖表)

以下是 C# 結構與類別之間的 14 個最大差異。

C# 結構與類別

C# 結構與類別之間的主要差異

C# 結構體和類別之間的一些主要區別如下:

  1. 可以使用「struct」關鍵字聲明結構,而可以使用「class」關鍵字聲明類別。
  2. 結構體是值類型,因此結構體類型變數直接由結構體資料組成,而類別是引用類型,類別類型變數由對資料的引用組成,我們稱之為類別的物件。
  3. 類別類型變數在堆疊中分配,可以被垃圾回收,而結構類型變數在堆疊中分配或在包含類型中內聯分配。
  4. 類別物件是使用「new」關鍵字建立的,而結構物件可以使用或不使用「new」關鍵字建立。不使用「new」運算子實例化結構不允許使用者存取其方法、屬性或事件。
  5. struct 中的每個變數都包含其資料副本(ref 和out 參數變數除外),因此對一個變數執行的修改不會影響另一個變量,而在類別中,兩個或多個變數可以引用同一個對象,並且對一個變數執行的任何修改都可以引用一個變數可以影響另一個變數。我們可以透過下面的例子來理解這一點。

使用結構的範例

代碼:

using System;
namespace structAndClass
{
//creating structure
public struct Demo
{
public int x, y;
//parameterized constructor
public Demo(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class StructDemo
{
public static void Main(string[] args)
{
Demo a = new Demo(50, 50);
Demo b = a;
a.x = 100;
Console.WriteLine("Value of a.x = "+a.x);
Console.WriteLine("Value of b.x = "+b.x);
}
}
}
登入後複製

輸出:

C# 結構與類別

使用類別的範例

代碼:

using System;
namespace structAndClass
{
public class Demo
{
public int x, y;
public Demo(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class StructDemo
{
public static void Main(string[] args)
{
Demo a = new Demo(50, 50);
Demo b = a;
a.x = 100;
Console.WriteLine("Value of a.x = "+a.x);
Console.WriteLine("Value of b.x = "+b.x);
}
}
}
登入後複製

輸出:

C# 結構與類別

  1. 結構類型的記憶體分配和釋放比類別類型便宜。
  2. 結構不能有無參數實例建構函數,它可以有參數化或靜態建構函數,而類別可以有預設的無參數建構函數。
  3. 結構不能有析構函數,而類別可以有析構函數。
  4. 我們不能從另一個結構或類別繼承一個結構,它不能是一個類別的基類,而我們可以從另一個類別繼承一個類,一個類別可以是另一個類別的基底類別。因此,類別支援繼承,而結構不支援繼承。
  5. 我們不能將結構的成員指定為抽象、虛擬或受保護,而類別可以將其成員指定為抽象、虛擬或受保護。
  6. 類別的實例稱為對象,而結構的實例稱為結構變數。
  7. 如果我們沒有指定任何存取說明符,那麼類別的成員預設是私有的,而結構的成員預設是公共的。
  8. 類別用於複雜的資料結構,而結構用於小型資料結構。

C# 結構與類別比較表

讓我們透過一個比較表來看看 C# Struct 與 Class 之間的更多差異,以便清楚地理解:

Parameter C# Struct Class
Data type The structure is a value type of data type. Class is a reference type data type.
Keyword The structure can be defined using the ‘struct’ keyword. The class can be defined using the ‘class’ keyword.
Storage area The structure variable is stored either in stack or inline in containing type. The object of the class is stored in heap.
Instance creation The instance of a struct can be created with or without a ‘new’ keyword. The instance of the class is created using a ‘new’ keyword.
Assignment If we make an assignment to a variable of struct type then it creates a copy of the value being assigned. Thus, each variable instructs has its copy of data. If we make an assignment to a variable of class type then it copies the reference. Thus, two or more variables in class can refer to the same object.
Constructor The structure does not contain a parameterless instance constructor. The class contains a parameterless instance constructor, if not defined explicitly by the user.
Destructor It cannot have a destructor. It can have a destructor.
Inheritance The structure cannot inherit from another class or structure.

The structure cannot be used as a base for another structure or class.

The class can inherit from another class. The class can be a base class for another class.
Data members Members of a struct cannot be abstract, virtual or protected. Members of a class can be abstract, virtual or protected.
Instance The instance of the structure is called the structure variable. The instance of the class is called an object.
Default access specifier Members of the structure are public by default. Members of the class are private by default.
Usage The structure can be used for small data structures. Class is used for the complex data structure.
Garbage collection The instance of a struct cannot be garbage collected. The instance of the class can be garbage collected.
Variable initializing Including variable initializer is not allowed while making instance field declarations instruct. Including variable initializer is allowed while making instance field declarations in a class.

結論

類別和結構體都用來保存相關的資料項。對於大量資料我們可以使用類,對於小數據我們可以使用結構。類別是參考類型,儲存在堆中,而結構是值類型,儲存在堆疊中。

以上是C# 結構與類別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!