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年工作经验,喜欢钻研,喜欢创新,闲暇之余喜欢写写博客,深知自身能力薄弱,如表达错误.不当之处请园友们多多指出,互相交 ...
随机推荐
- 性能优化-屏幕常亮与CPU唤醒
Android在不使用的时候,屏幕在一段时间以后会变暗,再过一段时间就会熄屏,此时CPU就会休眠,那么在这个时候,Timer.Handler.Thread.Service等都会暂停,有时候我们需要屏幕 ...
- Java Netty和Android之WebSocket,Springboot和Vue项目网址
在Netty上使用Websocket和网页上写个简单的websocket https://www.cnblogs.com/amibandoufu/p/11442881.html Android上使用w ...
- 我的vim开发环境搭建:C/C++/Go,持续更新中
懒得在github博客上折腾评论功能,先借用博客园推广下,虽然好像也没什么用. 我的vim开发环境搭建(1): 准备工作 我的vim开发环境搭建(2): 常用的vim插件 我的vim开发环境搭建(3) ...
- js延迟2秒执行事件
有时候,我们在做修改回显数据时,就需要默认触发一些事件,但是由于数据没有很快从服务器中取回,所以就有延迟执行js事件 setTimeout(function () { // 这里就是处理的事件 }, ...
- STM32PWM之——定时器(1)
定时器功能简介 STM32 一共有 8 个都为 16 位的定时器.其中 TIM6. TIM7 是基本定时器:TIM2.TIM3. TIM4. TIM5 是通用定时器: TIM1 和 TIM8 是高级定 ...
- Jquery对表单、表格的操作以及应用
表单的应用 (1)表单标签:包含处理表单数据所用的服务器端程序URL以及数据提交到服务器的方法 (2)表单域:包含文本框.密码框.隐藏域.多行文本框.复选框.单选框.下拉选择框.和文件上传框 (3)表 ...
- zabbix监控mysql主从同步和延迟
https://blog.csdn.net/natmazz/article/details/90581490 https://www.cnblogs.com/01-single/p/10602610. ...
- Ember.js和Vue.js,哪种框架更适合你?
JavaScript最初是为Web应用程序而创建的.随着前端技术的发展,比起纯JavaScript 脚本,大多数开发人员更喜欢使用基于JavaScript的框架来开发Web应用,如Vue.React等 ...
- 如何使用RedisTemplate访问Redis数据结构之Hash
Redis的Hash数据机构 Redis的散列可以让用户将多个键值对存储到一个Redis键里面. public interface HashOperations<H,HK,HV> Hash ...
- WMIC命令的利用技巧
WMIC扩展WMI(Windows Management Instrumentation,Windows管理工具),提供了从命令行接口和批命令脚本执行系统管理的支持.在WMIC出现之前,如果要管理WM ...