c#利用循环将类实例化为对象
上面的代码每次使用前并没有实例化,会报错,实例化以下就好了,参考:http://bbs.csdn.net/topics/391938383
c#实例化对象
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace array1 7 { 8 public class student 9 { 10 public string name = null; 11 public decimal number; 12 public string sex = null; 13 } 14 class Program 15 { 16 static void Main(string[] args) 17 { 18 student[] stu = new student[2]; 19 for (int i = 0; i < 2; i++) 20 { 21 stu[i] = new student(); 22 Console.WriteLine("请输入姓名:"); 23 stu[i].name = Console.ReadLine(); 24 Console.WriteLine("请输入序号:"); 25 stu[i].number = int.Parse(Console.ReadLine()); 26 Console.WriteLine("请输入性别:"); 27 stu[i].sex = Console.ReadLine(); 28 } 29 30 Console.ReadKey(); 31 } 32 } 33 }