结构体与枚举类型

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

一、定义的方法:

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;
        }

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

上课内容:
public  struct student//如果想让其他添加出来的类也能够使用此结构体,需要在前面加上public
        {
            public int nianling;//想让其他的类可以访问到其中的变量需要加上public
            public string name;
            public string sex;
            public One qq;//可以结构体中包含另一个结构体
            public string[] shuzu;//可以直接定义一个数组,但是没有开辟空间
        }
        public struct One
        {
            public string nb;
            public string abc;
        }
        static void Main(string[] args)
        {

            #region
            //为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称=值)
            student st = new student();//使用之前需要先初始化一下
            st.name = "张三";//初始化出来的变量名可以看做一个类对象
            st.nianling = 21;//类对象的名称是不能相同的
            st.sex = "男";
            st.name = "王五";
            //使用的时候利用变量名点出来其中的变量进行使用
            Console.WriteLine(st.name);
            st.qq.abc="qsqs";//结构体中包含另一个结构体类型,可以直接点出来一以下的变量
            st.shuzu = new string [9];//使用之前需要先开辟空间
            st.shuzu[0] = "赵六";//数组元素赋值方式

            student st1 = new student();//可以多次初始化类,注意不同的变量名
            st1.name = "李四";
            st1.nianling = 22;
            st1.sex = "女";
            #endregion
        }

枚举类型:
1.枚举类型只针对字符串,对于索引,无意义
2.常量的集合,这些常量只能取值,不能赋值
3.用常量表示所引用的字符串,这样可以省去重复写入长字符串

 练习:
20人投票,五个候选人,用switch  case

投票的时候输入1,2,3,4,5
利用12345来确定是哪一个候选人得票
计算得票数
得票最高的胜出

原文地址:https://www.cnblogs.com/shi2172843/p/5635537.html