Rumah >pembangunan bahagian belakang >Tutorial C#.Net >C# Struct vs Class
Seperti namanya, C# menggunakan kata kunci 'struct' untuk menentukan jenis nilai, dan Kelas menggunakan kata kunci 'class' untuk menentukan jenis rujukan. Dalam C#, pembolehubah yang ditakrifkan dalam struktur disimpan dalam tindanan atau dalam jenis pembolehubah yang diberikan dan kejadian dipanggil sebagai pembolehubah struktur. Manakala, untuk 'kelas' keadaan dipanggil sebagai objek dan disimpan dalam unit memori berstruktur timbunan. Dari segi pembina dan pemusnah, C# struct tidak boleh mempunyai pemusnah, tetapi Kelas boleh mempunyai pemusnah. Pengelasan ahli dibenarkan dalam Kelas, seperti jenis abstrak, maya dan dilindungi.
Di bawah ialah 14 perbezaan teratas antara C# Struct vs Class.
Beberapa perbezaan utama antara C# Struct dan Class adalah seperti berikut:
Kod:
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); } } }
Output:
Kod:
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); } } }
Output:
Mari kita lihat beberapa lagi perbezaan antara C# Struct vs Class melalui jadual perbandingan untuk pemahaman yang jelas:
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. |
Kedua-dua kelas dan struktur digunakan untuk menyimpan item data berkaitan. Kita boleh menggunakan kelas untuk sejumlah besar data dan boleh menggunakan struktur untuk data kecil. Kelas adalah jenis rujukan dan disimpan dalam timbunan manakala struktur adalah jenis nilai dan disimpan dalam tindanan.
Atas ialah kandungan terperinci C# Struct vs Class. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!