- 新建控制台应用程序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();
}
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。