码迷,mamicode.com
首页 > 其他好文 > 详细

委托事件

时间:2018-07-23 22:40:55      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:click   tun   event   ssid   space   summary   src   rabl   ide   

1.解耦

技术分享图片
using System.Collections.Generic;

namespace MyDelegate
{
    public class ListExtend
    {
        #region 数据准备
        /// <summary>
        /// 准备数据
        /// </summary>
        /// <returns></returns>
        public List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="老K",
                    ClassId=2,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="hao",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="大水",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="半醉人间",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="风尘浪子",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="一大锅鱼",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="小白",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="yoyo",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="冰亮",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="毕帆",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一点半",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="小石头",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="大海",
                    ClassId=2,
                    Age=30
                },
                 new Student()
                {
                    Id=3,
                    Name="yoyo",
                    ClassId=3,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="unknown",
                    ClassId=4,
                    Age=30
                }
            };
            #endregion
            return studentList;
        }
        #endregion

        #region 部分检索
        public List<Student> SelectStuAge()
        {
            List<Student> stuList = this.GetStudentList();
            List<Student> list = new List<Student>();
            foreach (var item in stuList)
            {
                if (item.Age > 25)
                {
                    list.Add(item);
                }
            }
            return list;
        }
        public List<Student> SelectStuName()
        {
            List<Student> stuList = this.GetStudentList();
            List<Student> list = new List<Student>();
            foreach (var item in stuList)
            {
                if (item.Name.Length > 2)
                {
                    list.Add(item);
                }
            }
            return list;
        }
        public List<Student> SelectStuClassId()
        {
            List<Student> stuList = this.GetStudentList();
            List<Student> list = new List<Student>();
            foreach (var item in stuList)
            {
                if (item.ClassId == 2)
                {
                    list.Add(item);
                }
            }
            return list;
        }
        #endregion
        #region 委托检索
        public delegate bool SelectStuDelegate(Student student);
        public bool SelectStuByAge(Student student)
        {
            if (student.Age > 25)
            {
                return true;
            }
            return false;
        }
        public bool SelectStuByName(Student student)
        {
            if (student.Name.Length > 2)
            {
                return true;
            }
            return false;
        }
        public bool SelectStuByClassId(Student student)
        {
            if (student.ClassId == 2)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 委托解耦,减少重复代码
        /// </summary>
        /// <param name="stuList"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public IEnumerable<Student> SelectStu(IEnumerable<Student> stuList, SelectStuDelegate method)
        {
            //return stuList.Where(n => method(n));
            List<Student> list = new List<Student>();
            foreach (var item in stuList)
            {
                if (method(item))
                {
                    list.Add(item);
                }
            }
            return list;
        }
        #endregion
    }
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ClassId { get; set; }
        public int Age { get; set; }
    }
}
View Code

 

委托事件

标签:click   tun   event   ssid   space   summary   src   rabl   ide   

原文地址:https://www.cnblogs.com/Jacob-Wu/p/9357241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!