C# の構造体とクラス

WBOY
リリース: 2024-09-03 15:07:59
オリジナル
718 人が閲覧しました

名前のとおり、C# は「struct」キーワードを使用して値の型を定義し、Class は「class」キーワードを使用して参照型を定義します。 C# では、構造体で定義された変数はスタックまたは指定された変数型に格納され、そのインスタンスは構造体変数と呼ばれます。一方、「クラス」の場合、インスタンスはオブジェクトと呼ばれ、ヒープ構造のメモリユニットに格納されます。コンストラクターとデストラクターに関しては、C# struct はデストラクターを持つことができませんが、クラスはデストラクターを持つことができます。クラスでは、抽象型、仮想型、保護型などのメンバー分類が許可されます。

C# 構造体とクラスの直接の比較 (インフォグラフィックス)

以下は、C# 構造体とクラスのトップ 14 の違いです。

C# の構造体とクラス

C# 構造体とクラスの主な違い

C# の構造体とクラスの主な違いは次のとおりです。

  1. 構造体は「struct」キーワードを使用して宣言でき、クラスは「class」キーワードを使用して宣言できます。
  2. 構造体は値型であるため、構造体型変数は直接構造体データで構成されますが、クラスは参照型であり、クラス型変数はデータへの参照で構成されます。これをクラスのオブジェクトと呼びます。
  3. クラス型変数はヒープに割り当てられ、ガベージ コレクションが可能ですが、構造体型変数はスタックまたは包含型のインラインに割り当てられます。
  4. クラスオブジェクトは「new」キーワードを使用して作成されますが、構造体のオブジェクトは「new」キーワードの有無にかかわらず作成できます。 「new」演算子を使用せずに構造体をインスタンス化すると、ユーザーはそのメソッド、プロパティ、イベントにアクセスできなくなります。
  5. 構造体の各変数にはデータのコピーが含まれているため (ref パラメーター変数と out パラメーター変数を除く)、1 つの変数に対して実行された変更が別の変数に影響を与えることはありませんが、クラスでは 2 つ以上の変数が同じオブジェクトを参照でき、変更はその変数に対して実行されます。ある変数が別の変数に影響を与える可能性があります。この点は、以下の例で理解できます。

struct を使用した例

コード:

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# の構造体とクラスの違いをさらに見てみましょう:

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!