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学习者快速成长!