c#系统泛型委托
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=, Name="001小王",Age= },
new UserInfo (){ Id=,Name="002大王",Age=},
new UserInfo (){ Id=,Name="003笨笨",Age=},
new UserInfo (){ Id=,Name="004通天塔",Age=},
};
}
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();
}
}
}
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=, Name="001小王",Age= },
new UserInfo (){ Id=,Name="002大王",Age=},
new UserInfo (){ Id=,Name="003笨笨",Age=},
new UserInfo (){ Id=,Name="004通天塔",Age=},
};
}
static void Main(string[] args)
{
#region
List<UserInfo> list = getInit();
list = list.FindAll(new Predicate<UserInfo>(delegate (UserInfo u) { return u.Id > ; }));
list.ForEach(u => {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.WriteLine("----------------------------------------------------------------------------");
list = list.FindAll(delegate (UserInfo u) { return u.Id > ; });
list.ForEach(u => {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.WriteLine("----------------------------------------------------------------------------");
list=list.FindAll(u => u.Id > );
list.ForEach(u=> {
Console.WriteLine(u.Id + "|" + u.Name);
});
#endregion Console.WriteLine("----------------------------自定义扩展方法---------------------------");
List<UserInfo> listnew = list.MyFindAll<UserInfo>(delegate (UserInfo u) { return u.Id > ; });
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;
}
}
}
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=, Name="001小王",Age= },
new UserInfo (){ Id=,Name="002大王",Age=},
new UserInfo (){ Id=,Name="003笨笨",Age=},
new UserInfo (){ Id=,Name="004通天塔",Age=},
};
}
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();
}
}
}
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=, Name="001小王",Age= },
new UserInfo (){ Id=,Name="002大王",Age=},
new UserInfo (){ Id=,Name="003笨笨",Age=},
new UserInfo (){ Id=,Name="004通天塔",Age=},
};
}
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;
}
}
}
c#系统泛型委托的更多相关文章
- 匿名方法、Lambda表达和自定义泛型委托以及Func、Action系统泛型委托
1.匿名方法的概念:一个方法没有具体的名称,而只有关键字delegate.方法参数.方法体.这种方法是匿名方法. 匿名方法的好处:将具体方法和委托直接关联在一起,如果我们基于委托只需要一个方法的时候, ...
- 委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式
引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录 ...
- 使用.NET中的Action及Func泛型委托
委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调 ...
- 泛型委托及委托中所涉及到匿名方法、Lambda表达式
泛型委托及委托中所涉及到匿名方法.Lambda表达式 引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到 ...
- 第十节:委托和事件(2)(泛型委托、Func和Action、事件及与委托的比较)
一. 泛型委托 所谓的泛型委托,即自定义委托的参数可以用泛型约束,同时内置委托Func和Action本身就是泛型委托. 将上一个章节中的Calculator类中的方法用自定义泛型委托重新实现一下. p ...
- 泛型委托Func<T>
Func<T>——委托只有泛型版本的,接受参数个数可以是若干个,也可以没有,但是必须是有返回值的方法. Func<TResult>——这个表示没有参数,只有返回值TResult ...
- 泛型委托Action与ActionT
以前都是自己写委托,其实系统内部给我们系统了委托的. Action ——委托的非泛型版本就是一个无参数无返回值的委托. Action<T>——委托的泛型版本是一个无返回值,但是参数个数及类 ...
- c# 匿名方法(函数) 匿名委托 内置泛型委托 lamada
匿名方法:通过匿名委托 .lamada表达式定义的函数具体操作并复制给委托类型: 匿名委托:委托的一种简单化声明方式通过delegate关键字声明: 内置泛型委托:系统已经内置的委托类型主要是不带返回 ...
- c#委托、泛型委托和匿名方法
题外话:别指望看第一遍书就能记住和掌握什么——请看第二遍.第三遍. 本人女猿一枚,2年工作经验,喜欢钻研,喜欢创新,闲暇之余喜欢写写博客,深知自身能力薄弱,如表达错误.不当之处请园友们多多指出,互相交 ...
随机推荐
- Mysql性能优化之---(二)
建立适当的索引 说起提高数据库性能,索引是最物美价廉的东西了.不用加内存,不用改程序,不用调sql,只要执行个正确的'create index',查询速度就可能提高百倍千倍,这可真有诱惑力.可是天下没 ...
- Holy Grail【spfa求最短路】
题目链接:https://www.jisuanke.com/contest/3004?view=challenges 题目大意: 1.一个无向图,给出六个顶点,添六条边,但是添边是有限制的.每次添边的 ...
- 《xv6 Appendices: PC Hardware and Boot loader》学习笔记
MIT 6.828 Lecture 2的preparation要求阅读<xv6 book>的附录部分,附录包括"PC Hardware"和"The Boot ...
- [Cometoj#4 C]方块切割_质因数分解_贪心
方块切割 题目链接:https://cometoj.com/contest/39/problem/C?problem_id=1583 数据范围:略. 题解: 首先,如果我们知道了多少道在行上,多少刀在 ...
- js中的三目运算符详解
判断 javascript中的三目运算符用作判断时,基本语法为: expression ? sentence1 : sentence2 当expression的值为真时执行sentence1,否则执行 ...
- Python 命名规范总结
Python推荐命名规范: 模块名和包名采用小写字母并且以下划线分隔单词的形式: 如:browser_driver 类名或异常名采用每个单词首字母大写的方式: 如:BasePage, Keyboard ...
- linux tcp listen函数的参数backlog
1 listen函数(http://man7.org/linux/man-pages/man2/listen.2.html) int listen(int sockfd, int backlog); ...
- 利用神器BTrace 追踪线上 Spring Boot应用运行时信息
概述 生产环境中的服务可能会出现各种问题,但总不能让服务下线来专门排查错误,这时候最好有一些手段来获取程序运行时信息,比如 接口方法参数/返回值.外部调用情况 以及 函数执行时间等信息以便定位问题.传 ...
- 【AC自动机】最短母串
[题目链接] https://loj.ac/problem/10061 [题意] 给定 n 个字符串 S1-Sn,要求找到一个最短的字符串 T,使得这 n 个字符串都是 T 的子串. [题解] 类似于 ...
- UML学习(四)-----状态图
状态图主要用于描述对象具有的各种状态.状态之间的转换过程以及触发状态转换的各种事件和条件. 1.状态图的组成 1.1 状态 主要用于描述一个对象在生命周期内的一个时间段.状态图中的状态包括状态名.内部 ...