- 新建控制台应用程序ListDemo
- 新建Student类
namespace ListDemo
{
/// <summary>
/// 学生类
/// </summary>
class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
public string PhoneNumber { get; set; }
public string StuAddress { get; set; }
}
}
namespace ListDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> stuList = new List<Student>();//创建泛型集合
Student objStu1 = new Student() { StudentId = 1001, StudentName = "小王", Age = 20 };
Student objStu2 = new Student() { StudentId = 1001, StudentName = "小张", Age = 20 };
Student objStu3 = new Student() { StudentId = 1001, StudentName = "小刘", Age = 20 };
Student objStu4 = new Student() { StudentId = 1001, StudentName = "小李", Age = 20 };
Student objStu5 = new Student() { StudentId = 1001, StudentName = "小朱", Age = 20 };
//添加对象到容器
stuList.Add(objStu1);
stuList.Add(objStu2);
stuList.Add(objStu3);
stuList.Add(objStu4);
stuList.Add(objStu5);
for (int i = 0; i < stuList.Count;i++ )
{
Console.WriteLine(stuList[i].StudentName+"\t"+stuList[i].Age+"\t"+stuList[i].StudentId);
}
Console.ReadKey();
}
}
}