对集合进行封装,只把需要的接口暴露给外部使用者,例如AddStudent,RemoveStudent 以及属性Count 而不是把整个集合暴露出来让外部使用者去操作 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleTest { public class MyCollection { public MyCollection() { list = new List<StudentInfo>(); } private List<StudentInfo> list = null; public void AddStudent(StudentInfo studentInfo) { list.Add(studentInfo); } public void RemoveStudent(StudentInfo studentInfo) { if (list.Contains(studentInfo)) { list.Remove(studentInfo); } } public int Count { get { return list.Count; } } } public class StudentInfo { public int Id{get;set;} public string Name{get;set;} } }