扩展IEnumerable<T> ForEach()方法
相信很多人,在用Linq时,都会困惑为什么IEnumerabel<T>没有ForEach,虽然 我们一样可以这样写,很快读写
foreach(item in items)
{
Console.Write(item);
}
但是相信总有人,会感觉不平衡吧,List<T> 就可以很轻松的ForEach()就可以.而此时不行.很是失望吧.
呵呵.添加如下代码扩展方法,申明在空间 System.Linq 下.这样你就在直接调用IEnumerable<T>.ForEach().
namespace System.Linq;
{
public static class EnumerableExtensionMethods
{
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (action == null)
throw new ArgumentNullException("action"); foreach (var item in source)
action(item); return source;
}
}
}
扩展IEnumerable<T> ForEach()方法的更多相关文章
- 为IEnumerable扩展一个ForEach方法
IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法: Source Code: using System; using System.Collections.Generi ...
- 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...
- IEnumerable<T>作为方法返回值类型——建议通过yield return返回
若IEnumerable<T>作为方法返回值的类型,则建议使用“迭代”模式(yield return) private IEnumerable<TwoLevelTreeNodeVie ...
- ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法
ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法 (2016-10-21 16:49:53) 今天在做一个查询的时候,报了一个"ORA-01652无法通过 ...
- C# List.ForEach 方法
C#中List.ForEach 方法是对 List 的每个元素执行指定操作. 示例: using System; using System.Collections.Generic; using Sys ...
- forEach 方法 (Array) (JavaScript)
为数组中的每个元素执行指定操作. 语法 array1.forEach(callbackfn[, thisArg]) 参数 参数 定义 array1 必选.一个数组对象. callbackfn 必选.最 ...
- Easyui扩展或者重载(方法和属性)
1: 使用$.fn.datagrid.defaults.editors重载默认值. 每个编辑器都有以下方法: 名称 属性 描述 init container, options 初始化编辑器并返回目标对 ...
- JavaScript forEach方法
最近看了一些html5和js方面的书,受益匪浅,因为看的东西比较多,却都没有怎么静心来做整理,慢慢来吧,可能最近自己有点儿小紧张.今天跟大家分享下JavaScript的forEach方法(其实是从&l ...
- php安装扩展的几种方法
转自:http://doc3.workerman.net/appendices/install-extension.html 安装扩展 注意 与Apache+PHP或者Nginx+PHP的运行模式不同 ...
随机推荐
- ubuntu 删除 mysql (转)
1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server 3 sudo apt-get ...
- 2.2_springboot2.x消息RabbitMQ整合&amqpAdmin管理组件的使用
5.1.1.基本测试 1.引 spring-boot-starter-amqp** <dependencies> <dependency> <groupId>org ...
- Ubuntu16.04下安装Visual Studio Code
sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubun ...
- drupal7 smtp+mimemail+mailsystem 实现发送html邮件
1.下载三个模块 smtp: https://www.drupal.org/project/smtp mimemail: https://www.drupal.org/project/mimemail ...
- leetcode-973-最接近原点的K个点
题目描述: 可参考:题215 方法一:排序 class Solution: def kClosest(self, points: List[List[int]], K: int) -> List ...
- ros清理日志文件
检查日志文件: rosclean check 清理日志文件: rosclean purge
- jquery学习笔记(五):AJAX
内容来自[汇智网]jquery学习课程 5.1 ajax AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新. AJAX = 异步 JavaScript 和 XML ...
- RPC 编程
我们从一个简单的 RPC "Hello, world!"的例子开始. 参考资料:MSDN: Win32 and COM Development -> Networking - ...
- MongoDB错误记录
文章目录 mongoDB启动报错 mongoDB启动报错 在bin目录下执行 ./mongod 报错如下 F CONTROL [main] Failed global initialization: ...
- Spring源码由浅入深系列一 简介
概述: Spring是一个企业级的开源框架.它提供轻量级的依赖注入.面向切面编程.全方位的整合框架.下图是Spring框架的组成部分,各部分内容作了简单说明. 依赖注入: 依赖注入是S ...