C# 结构体(Struct)
在 C# 中,结构体(struct)是一种值类型(value type),用于组织和存储相关数据。
在 C# 中,结构体是值类型数据结构,这样使得一个单一变量可以存储各种数据类型的相关数据。
struct 关键字用于创建结构体。
结构体是用来代表一个记录,假设您想跟踪图书馆中书的动态,您可能想跟踪每本书的以下属性:
- Title
- Author
- Subject
- Book ID
实例
using System;
using System.Text;
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1; /* 声明 Book1,类型为 Books */
Books Book2; /* 声明 Book2,类型为 Books */
/* book 1 详述 */
Book1.title = "C Programming";
Book1.author = "Nuha Ali";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 详述 */
Book2.title = "Telecom Billing";
Book2.author = "Zara Ali";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 6495700;
/* 打印 Book1 信息 */
Console.WriteLine( "Book 1 title : {0}", Book1.title);
Console.WriteLine("Book 1 author : {0}", Book1.author);
Console.WriteLine("Book 1 subject : {0}", Book1.subject);
Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);
/* 打印 Book2 信息 */
Console.WriteLine("Book 2 title : {0}", Book2.title);
Console.WriteLine("Book 2 author : {0}", Book2.author);
Console.WriteLine("Book 2 subject : {0}", Book2.subject);
Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);
Console.ReadKey();
}
}
注释部分演示了结构不能包含无参数的构造函数、不能继承以及结构的实例复制是复制整个结构的内容。与之相反,类可以包含无参数的构造函数,可以继承,并且实例复制是复制引用。
实例
using System;
// 结构声明
struct MyStruct
{
public int X;
public int Y;
// 结构不能有无参数的构造函数
// public MyStruct()
// {
// }
// 有参数的构造函数
public MyStruct(int x, int y)
{
X = x;
Y = y;
}
// 结构不能继承
// struct MyDerivedStruct : MyBaseStruct
// {
// }
}
// 类声明
class MyClass
{
public int X;
public int Y;
// 类可以有无参数的构造函数
public MyClass()
{
}
// 有参数的构造函数
public MyClass(int x, int y)
{
X = x;
Y = y;
}
// 类支持继承
// class MyDerivedClass : MyBaseClass
// {
// }
}
class Program
{
static void Main()
{
// 结构是值类型,分配在栈上
MyStruct structInstance1 = new MyStruct(1, 2);
MyStruct structInstance2 = structInstance1; // 复制整个结构
// 类是引用类型,分配在堆上
MyClass classInstance1 = new MyClass(3, 4);
MyClass classInstance2 = classInstance1; // 复制引用,指向同一个对象
// 修改结构实例不影响其他实例
structInstance1.X = 5;
Console.WriteLine($"Struct: {structInstance1.X}, {structInstance2.X}");
// 修改类实例会影响其他实例
classInstance1.X = 6;
Console.WriteLine($"Class: {classInstance1.X}, {classInstance2.X}");
}
}
实例
using System;
using System.Text;
struct Books
{
private string title;
private string author;
private string subject;
private int book_id;
public void setValues(string t, string a, string s, int id)
{
title = t;
author = a;
subject = s;
book_id =id;
}
public void display()
{
Console.WriteLine("Title : {0}", title);
Console.WriteLine("Author : {0}", author);
Console.WriteLine("Subject : {0}", subject);
Console.WriteLine("Book_id :{0}", book_id);
}
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1 = new Books(); /* 声明 Book1,类型为 Books */
Books Book2 = new Books(); /* 声明 Book2,类型为 Books */
/* book 1 详述 */
Book1.setValues("C Programming",
"Nuha Ali", "C Programming Tutorial",6495407);
/* book 2 详述 */
Book2.setValues("Telecom Billing",
"Zara Ali", "Telecom Billing Tutorial", 6495700);
/* 打印 Book1 信息 */
Book1.display();
/* 打印 Book2 信息 */
Book2.display();
Console.ReadKey();
}
}