zoukankan      html  css  js  c++  java
  • c#系统泛型委托

    Action<T> 无返回值的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                list.ForEach(new Action<UserInfo>(delegate (UserInfo ui) { Console.WriteLine(ui.Name); }));
                Console.WriteLine("----------------------------------------------------------------------------");
                list.ForEach(delegate (UserInfo ui) { Console.WriteLine(ui.Id+"|"+ui.Name); });
                Console.WriteLine("----------------------------------------------------------------------------");
                list.ForEach(u=> {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
        }
    }
    Predicate<T> 返回bool值的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
       static  class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                #region
                List<UserInfo> list = getInit();
                list = list.FindAll(new Predicate<UserInfo>(delegate (UserInfo u) { return u.Id > 1; }));
                list.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                list = list.FindAll(delegate (UserInfo u) { return u.Id > 2; });
                list.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                list=list.FindAll(u => u.Id > 3);
                list.ForEach(u=> {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                #endregion
    
                Console.WriteLine("----------------------------自定义扩展方法---------------------------");
                List<UserInfo> listnew = list.MyFindAll<UserInfo>(delegate (UserInfo u) { return u.Id > 3; });
                listnew.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
    
            static List<T> MyFindAll<T>(this List<T> list, Predicate<T> predicate)
            {
                //新集合
                List<T> newlist = new List<T>();
               //遍历老集合
                foreach (T item in list)
                {
                    //如果item符合条件,则加入新集合
                    if (predicate(item))
                    {
                        newlist.Add(item);
                    }
                }
                return newlist;
            }
        }
    }
    Comparison<T> 返回int类型
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
       static  class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                list.Sort(delegate (UserInfo u1, UserInfo u2) {
                    return u2.Age - u1.Age;
                });
                list.ForEach(u =>
                {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
        }
    }
    Func<T,TReturn> 自定义返回类型的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        public class UserSimple
        {
            public string Name { get; set; }
        }
        static class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                IEnumerable<UserSimple> uslist = list.Select(new Func<UserInfo, UserSimple>(delegate (UserInfo u) { return new UserSimple() { Name = u.Name }; }));
                uslist.ToList().ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                IEnumerable<UserSimple> newuslist = list.Select(delegate (UserInfo u) { return new UserSimple() {  Name = u.Name }; });
                uslist.ToList().ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.WriteLine("-------------------------------------自定义-------------------------------");
                List<UserSimple> listnew = list.MySelect<UserInfo, UserSimple>(delegate(UserInfo u) { return new UserSimple() { Name = u.Name }; });
                listnew.ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.ReadLine();
            }
            static List<TR> MySelect<T1, TR>(this List<T1> list, Func<T1, TR> func)
            {
                List<TR> listnew = new List<TR>();
                foreach (T1 item in list)
                {
                    TR tr = func(item);
                    listnew.Add(tr);
                }
                return listnew;
            }
        }
    }
     
  • 相关阅读:
    fastreport 中文汉字换行
    Delphi调用自身onchange事件,如提示缺少声明object时,不能调用,用此方法!
    delphi 截取某个字符之前的字段,,如 1234567-9,要分别得到
    delphi 四舍五入取整函数
    SQLServer2008不允许保存更改错误解决办法_不允许保存更改、不允许修改表解决_百度经验
    关于APP的架构实现!微博和知乎中的 feed 流是如何实现的?
    win2003没有OLE DB Provider for SQLServer驱动,可安装sqlserver2000或者安装MDAC2.6,适用于winxp、win2003
    SQLServer2000安装失败,[ODBC 驱动程序管理器]未发现数据源,详细信息请查看日志文件 sql2000 [Microsoft][ODBC 驱动程序管理器] 未发现数据源,参见sqlstp.org,直接退出
    sql server2008R2 密钥
    CentOS7安装memcached
  • 原文地址:https://www.cnblogs.com/itmu89/p/7545440.html
Copyright © 2011-2022 走看看