泛型的筛选

namespace Test
{

    class Program
    {

        private static List<Student> list1 = new List<Student>();      //声明一个用于放置初始值的集合  注:必须声明List类型而不是IList接口类型
        private static List<Student> list2 = new List<Student>();      //用户存放筛选结果
        static void Main(string[] args)
        {

            //声明实例化student对象
            Student stu1 = new Student("aa", 10);

            Student stu2 = new Student("bb", 13);

            Student stu3 = new Student("cc", 15);

            Student stu4 = new Student("dd", 18);

            //向集合中添加对象
            list1.Add(stu1);

            list1.Add(stu2);

            list1.Add(stu3);

            list1.Add(stu4);

            Console.WriteLine(list1.Find(delegate(Student stu) { return stu.Age > 12; }).Name);            //返回筛选满足条件的第一个对象
            list2 = list1.FindAll(delegate(Student stu) { return stu.Age > 12; });          //对泛型集合进行筛选
            //遍历结果
            foreach (Student stu in list2)
            {

                Console.WriteLine(stu.Name);

            }

        }

    }

}
posted @ 2011-06-17 17:36  Frida-  阅读(529)  评论(0编辑  收藏  举报