委托事件

Posted jacob-wu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了委托事件相关的知识,希望对你有一定的参考价值。

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

 

以上是关于委托事件的主要内容,如果未能解决你的问题,请参考以下文章

jQuery代码优化:事件委托

C#事件

Javascript中的事件委托机制

js中的事件委托/代理

编写高质量代码改善C#程序的157个建议——建议137:委托和事件类型应添加上级后缀

事件委托