在 c# 中,结构体是一种值类型数据类型。它可以帮助您使单个变量保存各种数据类型的相关数据在一起。struct
关键字用于定义和创建一个结构。
结构体用于表示记录信息。假设您想在图书馆中跟踪记录书籍信息。假设希望跟踪每本书的以下属性:
要定义一个结构,需要使用struct
语句。struct
语句定义了一个新的数据类型,有多个程序的成员。
例如,以下是声明book
结构体的方式:
struct books
{
public string title;
public string author;
public string subject;
public int book_id;
};
以下示例程序显示如何使用结构体:
using system;
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; /* declare book1 of type book */
books book2; /* declare book2 of type book */
/* book 1 specification */
book1.title = "c programming";
book1.author = "maxsu";
book1.subject = "c programming tutorial";
book1.book_id = 5493427;
/* book 2 specification */
book2.title = "telecom billing";
book2.author = "sukida";
book2.subject = "telecom billing tutorial";
book2.book_id = 8493480;
/* print book1 info */
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);
/* print book2 info */
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();
}
}
当编译和执行上述代码时,会产生以下结果:
book 1 title : c programming
book 1 author : maxsu
book 1 subject : c programming tutorial
book 1 book_id :5493427
book 2 title : telecom billing
book 2 author : sukida
book 2 subject : telecom billing tutorial
book 2 book_id : 8493480
在上面地示例中,我们已经使用了一个名为books
的简单结构体。 c# 中的结构与传统的c语言或c++中的结构完全不同。 c# 结构具有以下特点:
new
运算符创建一个struct
对象时,它将调用相应的构造函数。 与类不同,可以在不使用new
运算符的情况下实例化结构体。new
运算符,则字段保持未分配,并且在所有字段初始化之前不能使用对象。类和结构有以下基本差别:
根据上述讨论,下面我们来重写上一个例子:
using system;
struct books
{
private string title;
private string author;
private string subject;
private int book_id;
public void getvalues(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(); /* declare book1 of type book */
books book2 = new books(); /* declare book2 of type book */
/* book 1 specification */
book1.getvalues("c programming",
"maxsu", "c programming tutorial", 749540712);
/* book 2 specification */
book2.getvalues("telecom billing",
"sukida", "telecom billing tutorial", 59570011);
/* print book1 info */
book1.display();
/* print book2 info */
book2.display();
console.readkey();
}
}
当编译和执行上述代码时,会产生以下结果:
title : c programming
author : maxsu
subject : c programming tutorial
book_id :749540712
title : telecom billing
author : sukida
subject : telecom billing tutorial
book_id :59570011