標籤:
結構體:自訂類型 實值型別
一組變數的組合
需要定義的位置 class裡面 main函數外面
裡麵包含的變數可以是多種資料類型的
例如學生資訊的結構體:學號,姓名,性別,分數
struct Student { public int xuehao; public string name; public string sex; public double score; }//執行個體化結構體 //Student st = new Student(); //st.xuehao = 1001; //st.name = "張三"; //st.sex = "男"; //st.score = 33; //Student st1 = new Student(); //st1.xuehao = 1002; //st1.name = "李四"; //st1.sex = "女"; //st1.score = 44;
還可以把分數再細分為語文成績,數學成績,英語成績
struct Student { public int xuehao; public string name; public string sex; public Score score; } struct Score { public double yufen; public double shufen; public double yingfen; } //ArrayList al = new ArrayList(); //Console.Write("請輸入班級人數:"); //int a = int.Parse(Console.ReadLine()); //for (int i = 0; i < a;i++ ) //{ // Student sst = new Student(); // Console.Write("請輸入第{0}個學生的學號:",(i+1)); // sst.xuehao = int.Parse(Console.ReadLine()) ; // Console.Write("請輸入第{0}個學生的姓名:", (i + 1)); // sst.name = Console.ReadLine(); // Console.Write("請輸入第{0}個學生的性別:", (i + 1)); // sst.sex = Console.ReadLine(); // Console.Write("請輸入第{0}個學生的分數:", (i + 1)); // sst.score = double.Parse(Console.ReadLine()); // al.Add(sst); //} //Console.WriteLine("所有人員資訊輸入完畢!請按斷行符號鍵開始列印!"); //Console.ReadLine(); //for (int i = 0; i < al.Count;i++ ) //{ // Student sst = (Student)al[i]; // Console.WriteLine("第{0}個學生的學號是:{1},姓名是{2},性別是{3},分數是{4}。",(i+1),sst.xuehao,sst.name,sst.sex,sst.score); //} ////執行個體化 //Student st = new Student(); //st.score.yufen = 77; //st.score.shufen = 88; //st.score.yingfen = 99;
c#部分---結構體;