结构体(structure)在 C# 中类似于类,用于存储数据。但是,与类不同,结构体是值类型。
假设我们要存储一个人的姓名和年龄。我们可以创建两个变量:name 和 age 并存储值。
然而,假设我们要存储多个人的相同信息。
在这种情况下,为每个人创建变量可能是一项繁琐的任务。为了解决这个问题,我们可以创建一个存储 name 和 age 的结构体。现在,这个结构体可以用于每个人。
在 C# 中定义结构体
在 C# 中,我们使用 struct
关键字来定义结构体。例如,
struct Employee {
public int id;
}
这里,id 是结构体中的一个字段。结构体也可以包含方法、索引器等。
声明结构体变量
在使用结构体之前,我们首先需要创建一个结构体变量。我们使用结构体名称和变量来声明结构体变量。例如,
struct Employee {
public int id;
}
...
// declare emp of struct Employee
Employee emp;
在上面的示例中,我们创建了一个名为 Employee 的结构体。在这里,我们声明了一个 Employee 结构体的变量 emp。
访问 C# 结构体
我们使用结构体变量和 .
运算符来访问结构体的成员。例如,
struct Employee {
public int id;
}
...
// declare emp of struct Employee
Employee emp;
// access member of struct
emp.id = 1;
在这里,我们使用了 Employee 结构体的变量 emp 和 .
运算符来访问 Employee 的成员。
emp.id = 1;
这会访问 Employee 结构体的 id 字段。
注意:像 int
、bool
、float
这样的基本数据类型在 C# 中是预定义的结构体。
示例:C# 结构体
using System;
namespace CsharpStruct {
// defining struct
struct Employee {
public int id;
public void getId(int id) {
Console.WriteLine("Employee Id: " + id);
}
}
class Program {
static void Main(string[] args) {
// declare emp of struct Employee
Employee emp;
// accesses and sets struct field
emp.id = 1;
// accesses struct methods
emp.getId(emp.id);
Console.ReadLine();
}
}
}
输出
Employee Id: 1
在上面的程序中,我们创建了一个名为 Employee 的结构体。它包含一个字段 id 和一个方法 getId()。
在 Program 类中,我们声明了 Employee 结构体的变量 emp。然后我们使用 emp 变量来访问类的字段和方法。
注意:我们也可以使用 new
关键字来实例化结构体。例如,
Employee emp = new Employee();
这里,这行代码调用了结构体的无参构造函数,并将所有成员初始化为默认值。
C# 结构体中的构造函数
在 C# 中,结构体也可以包含构造函数。例如,
struct Employee {
public int id;
// constructor
public Employee(int employeeId) {
id = employeeId
}
}
在这里,我们创建了一个带参数的构造函数 Employee()
,其参数为 employeeId。
注意:在 C# 9.0 或更低版本中,我们不能创建无参构造函数。
示例:C# 结构体中的构造函数
using System;
namespace CsharpStruct {
// defining struct
struct Employee {
public int id;
public string name;
// parameterized constructor
public Employee(int employeeId, string employeeName) {
id = employeeId;
name = employeeName;
}
}
class Program {
static void Main(string[] args) {
// calls constructor of struct
Employee emp = new Employee(1, "Brian");
Console.WriteLine("Employee Name: " + emp.name);
Console.WriteLine("Employee Id: " + emp.id);
Console.ReadLine();
}
}
}
输出
Employee Name: Brian Employee Id: 1
在上面的示例中,我们在 Employee 结构体中创建了一个带参数的构造函数。在构造函数中,我们分配了字段 id 和 name 的值。
注意这行:
Employee emp = new Employee(1, "Brian");
与 C# 类一样,我们使用 new
关键字来调用构造函数。这里,1 和 "Brian" 是传递给构造函数的参数,它们分别被赋给参数 employeeID 和 employeeName。"
注意:我们在带参数的构造函数中必须为结构体的每个字段分配值。例如,
// error code
public Employee(int employeeID, employeeName) {
id = employeeID;
}
这里,我们没有为 name 字段分配值。因此,代码将生成一个错误。
C# 结构体中的属性
我们也可以在 C# 结构体中使用属性。例如,
using System;
namespace CsharpStruct {
// defining struct
struct Employee {
public int id;
// creates property
public int Id {
// returns id field
get {
return id;
}
// sets id field
set {
id = value;
}
}
}
class Program {
static void Main(string[] args) {
// calls the constructor of struct
Employee emp = new Employee();
emp.Id = 1;
Console.WriteLine("Employee Id: " + emp.Id);
Console.ReadLine();
}
}
}
输出
Employee Id: 1
在上面的示例中,我们在 Employee 结构体中有 Id 属性。
get
方法返回 id 字段,set
方法为 id 字段赋值。
C# 中类和结构体的区别
在 C# 中,类和结构体看起来很相似。但是,它们之间存在一些区别。
类是引用类型,而结构体是值类型。例如,
using System;
namespace CsharpStruct {
// defining class
class Employee {
public string name;
}
class Program {
static void Main(string[] args) {
Employee emp1 = new Employee();
emp1.name = "John";
// assign emp1 to emp2
Employee emp2 = emp1;
emp2.name = "Ed";
Console.WriteLine("Employee1 name: " + emp1.name);
Console.ReadLine();
}
}
}
输出
Employee1 name: Ed
在上面的示例中,我们将 emp1 的值赋给了 emp2。emp2
对象引用与 emp1 相同的对象。因此,对 emp2 的更新会自动更新 emp1 的值。
这就是为什么类是引用类型。
与类相反,当我们把一个结构体变量赋给另一个时,结构体的值会被复制到被赋值的变量中。因此,更新一个结构体变量不会影响另一个。例如,
using System;
namespace CsharpStruct {
// defining struct
struct Employee {
public string name;
}
class Program {
static void Main(string[] args) {
Employee emp1 = new Employee();
emp1.name = "John";
// assign emp1 to emp2
Employee emp2 = emp1;
emp2.name = "Ed";
Console.WriteLine("Employee1 name: " + emp1.name);
Console.ReadLine();
}
}
}
输出
Employee1 name: John
当我们把 emp1 的值赋给 emp2 时,会创建一个新的值 emp2。这里,emp1 的值被复制到 emp2。因此,对 emp2 的更改不会影响 emp1。
这就是为什么结构体是值类型。
此外,结构体不允许继承,而继承是 C# 类的一个重要特性。