程序代码:
class Student//新建一个学生类,包含学生的信息
{
public Student(string name, string no , string sex, int age)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
this.No = no;
}
public string Name { get; set; }//姓名
public string No { get; set; }//学号
public string Sex { get; set; }
public int Age { get; set; }
}
class Students : CollectionBase//这是一个集合,为了方便的操作,当然也可以用泛型List<Student>
{
public Student this[int index] { get { return (Student)List[index]; } set { List[index] = value; } }
public void Add(Student value)
{
this.List.Add(value);
}
public int IndexOfByName(string name)//用姓名查找
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].Name == name)
return i;
}
return -1;
}
public int IndexOfByNo(string no)//用学号查找
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].No == no)
return i;
}
return -1;
}
public void Insert(int index, Student value)//插入新记录
{
this.List.Insert(index, value);
}
}