CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#结构体
C#结构体
作者:--    发布时间:2019-11-20

在 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

c# 结构体的特点

在上面地示例中,我们已经使用了一个名为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

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册