结构体、枚举类型——8月3日

一、结构体、美剧类型都是属于值类型

(一)结构体

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;//下面要用到ArrayList集合,就要先引用这个
namespace 结构体_枚举类型
{
    class Program
    {
        struct Student
        {
            public int no;
            public string name;
            public string sex;
            public double score;
        }

        //struct fenshu
        //{
        //    public ArrayList al;
        //}
        
        ////结构体
        //struct Student
        //{
        //    public int no;
        //    public string name;
        //    public string sex;
        //    public score fen;
        //}
        ////结构体内部可以直接放置理你个结构体的数据类型
        //struct score
        //{
        //    public double yu;
        //    public double shu;
        //    public double ying;
        //}
        static void Main(string[] args)
        {
           
            ////初始化结构体
            //Student st = new Student();
            //st.no = 20001;
            //st.name = "张三";
            //st.sex = "男";
            //st.score = 80;
            ////将每个人的信息放进ArrayList集合中
            ////面向对象思想
            //ArrayList al = new ArrayList();
            //al.Add(st);
            ////
            //Student st1 = new Student();
            //st.no = 20002;
            //st.name = "李四";
            //st.sex = "男";
            //st.score = 80;
            //al.Add(st1);

            //Student st = new Student();
            //st.no = 10001;
            //st.name = "张三";
            //st.sex = "男";
            //st.fen.yu = 77;
            //st.fen.shu = 88;
            //st.fen.ying = 99;


            //定义一个集合,把学号、姓名、性别、分数放进结构体,再把结构体放进集合
            ArrayList al = new ArrayList();
            Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            for (int i = 0; i < a; i++)
            {
                Student st = new Student();
                Console.Write("请输入第{0}个学生的学号:", i + 1);
                st.no = int.Parse(Console.ReadLine());
                Console.Write("请输入第{0}个学生的姓名:", i + 1);
                st.name = Console.ReadLine();
                Console.Write("请输入第{0}个学生的性别:", i + 1);
                st.sex = Console.ReadLine();
                Console.Write("请输入第{0}个学生的分数:", i + 1);
                st.score = double.Parse(Console.ReadLine());
                al.Add(st);
            }
            //查看3号索引位置的学生的所有信息
            //接收出来的时候需要接受成添加时候的数据类型(Student)
            Student st1 = (Student)al[3];
            //输出时要每个信息单独输出,一起输出现实的是结构体
            Console.WriteLine(st1.no);
            Console.WriteLine(st1.name);
            Console.WriteLine(st1.sex);
            Console.WriteLine(st1.score);
            Console.ReadLine();
            
        }
    }
}

1、用法:

在main主函数外面定义了一个名称为student的结构体,以便于main函数之中使用。student st = new student();//这句话是在main函数之中定义了一个名为st的student类型的结构体。下面开始为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称值)

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

2、#region    代码段    #endregion     中间的代码可以被隐藏

(二)枚举类型

枚举类型是自定义的一个常量数组。

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

定义:Enum....{E}

  枚举元素的默认基础类型为int。默认情况下,第一个枚举数的值为0,后面每个枚举数的值         依次递增1。

用法:

   枚举也可以指定数据类型,一般不指定

   在定义枚举类型时,可以选择基类型,但可以使用的基类型仅限于long、int、short和byte。

原文地址:https://www.cnblogs.com/juyangchao12/p/5734361.html