结构体

结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样。

一、定义的方法:

struct student

{

public int nianling;

public int fenshu;

public string name;

public string sex;

public int sum;

}

      以上的语句就是定义一个名称为student的结构体,其中包含int类型的年龄、分数、总和,和string类型的姓名、性别。

二、用法:

      在main主函数外面定义了一个名称为student的结构体,以便于main函数之中使用。

      student st = new student();//这句话是在main函数之中定义了一个名为st的student类型的结构体。

      下面开始为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称=值)

  main函数下

{

st.nianling=22;

st.fenshu=80;

st.name="小李";

}

赋值完成之后可以打印出被赋值的项。

三、结构体类型里面包含结构体类型:

      可以在此前的student的结构体中在定义一个结构体

           public shuxing sx;//代表一个shuxing结构体变量组
          }
        public struct shuxing
        {
            public double tizhong;
            public double shengao;
            public int nianling;
            public string hunfou;
        }

      这样就可以在用的时候省下再次初始化结构体。

示例一:

示例二:

示例三:

原文地址:https://www.cnblogs.com/qiqige77/p/4046475.html