首页 > 技术文章 > 结构体

franky2015 2015-07-14 18:24 原文

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

一、定义的方法:注:结构体一般定义在Main函数的上面,Class下面,作为一个类,参数前面加上public代表公用变量,(也可用protect-受保护的,private-私有的,这2个基本不用)

struct student//student是结构体的名字

{

public int +变量名;

public string +变量名;

public int  +变量名;

}

二、用法:

 

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

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

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

class Program
    {
        struct student
        {

            public int nianling;

            public int fenshu;

            public string name;

            public string sex;

            public int sum;

        }



        static void Main(string[] args)
        {
            student st = new student();

                st.nianling = 22;

                st.fenshu = 80;

                st.name = "小李";

            

        }
    }

三、结构体的使用:

1.使用结构体来定义变量
Student s1 = new Student();
2.给结构体变量赋值。
s1.Name = "";
s1.NO = "";

推荐阅读