为IEnumerable扩展一个ForEach方法

IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法:

Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Insus.NET.ExtendMethods
{
  public static class Enumerables
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (T item in source)
            {
                action(item);
            }
        }
    }
}

下例中,Insus.NET列举一个IEnumerable数据集,然后使用这个方法来获取其中的元素:

Source Code:

public IEnumerable<IDictionary<string, object>> Links()
        {
            var dict = new Dictionary<string, object>();
            dict["Index"] = "新产品介绍";
            dict["Manufacturing"] = "制造能力/流程";
            dict["DieCasting"] = "压铸";
            dict["Machining"] = "加工";
            dict["AssemblyFinishing"] = "组装&成品";
            dict["TestReliability"] = "测试和可靠性";
            dict["KeyCustomer"] = "关键客户";
            yield return dict;
        }

ForEach方法应用:

Source Code:

obj.Links().ForEach(delegate (IDictionary<string, object> dict) {
            foreach (KeyValuePair<string, object> kvp in dict)
            {
                    //kvp.Key
                    //kvp.Value
            }
        });

时间: 2024-08-10 19:15:59

为IEnumerable扩展一个ForEach方法的相关文章

.NET MVC3中扩展一个HtmlHelper方法CheckBoxList

MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList) { return CheckBoxList(helper, name, selectList, new { });

飞波拿鸡(要定义IEnumerable&lt;T&gt;的ForEach扩展方法):

var N = 20; var list = Enumerable.Range(0, N).ToArray(); N = list.ForEach(n => list[n] = n < 2 ? n : list[n - 1] + list[n - 2]).Last(); 飞波拿鸡(要定义IEnumerable<T>的ForEach扩展方法):

数组方法的扩展,如map,reduce,fliter,forEach方法

map方法 该方法可以看为映射关系 let arr = [1,2,3,4,4,6,7] let result = arr.map(item => item*3 ) console.log(result) // 具体例子 let score = [50,54,60,70] let rank = score.map(item => item>=60?'合格':'不合格') console.log(rank) reduce 方法 // 数组中的reduce方法 可以看成汇总 let arr =

在ASP.NET MVC下扩展一个带验证的RadioButtonList

在ASP.NET MVC4中,HtmlHelper为我们提供了Html.RadioButton()方法用来显示Radio Button单选按钮.如果想显示一组单选按钮,通常的做法是遍历一个集合把每个单选按钮显示出来.本篇尝试写一个扩展方法用来展示一组带验证的单选按钮. 首先来扩展HtmlHelper,扩展方法中接收一个SelectListItem的集合,遍历这个集合把每个单选按钮显示出来,并且让这些单选按钮具有不同的id属性值. using System.Collections.Generic;

forEach方法(兼容所有浏览器)

//->自己在内置类的原型上扩展一个myForEach来处理forEach不兼容的问题//callBack:回调函数,遍历数组中的一项,就要执行一次callBack//context:改变callBack方法中的this指向 Array.prototype.myForEach = function myForEach(callBack, context) { typeof context === "undefined" ? context = window : null; if

扩展HtmlHelper辅助方法

1.什么是HtmlHelper辅助方法?其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class FormExtensions//表单相关扩展方法,例如创建表单标签等. public static class InputExtensions//这里包含了所有input,例如:text,button,readiobutton等等. public static class LinkExtensions

C# 索引器,实现IEnumerable接口的GetEnumerator()方法

当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [修饰符] 数据类型 this[索引类型 index] 以下是代码 1 /// <summary> 2 /// 单元格 3 /// </summary> 4 public class Cell 5 { 6 /// <summary> 7 /// Value 8 /// <

Junit4.x扩展:运行指定方法

相信很多道友搞开发的一般都会用到Junit单元测试工具,不知道大家有没有遇到一个这样的问题: 有的单元测试用例有很多@Test方法,甚至有的方法会执行很长时间,只能空等执行.而实际上我们只需要运行其中的某一些方法就可以了.然后有人会说不是有ingore注解么,可ingore需要为许多的方法添加,当测试方法达到一定数量级的时候,改起来会很烦躁,如果commit到代码服务器上甚至可能会影响别人工作.己所不欲... 之前有朋友跟我说过TestNG是支持指定执行哪些方法,本人没有亲自去实验,因为公司统一

利用jQuery来扩展一个瀑布流插件

  简单了解jQuery.fn.extend() jQuery.fn.extend()函数用于为jQuery扩展一个或多个实例属性和方法(主要用于扩展方法). (截图来自jQuery文档) 为了更清晰的理解我将需求写成了注释 扩展代码如下 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 1 (function($){ 2 3 $.fn.WaterFall = function () { 4 5 /* 6 * 瀑布流插件 7 * 容器的宽度固定 8