zoukankan      html  css  js  c++  java
  • 扩展IEnumerable<T> ForEach()方法 joe

        相信很多人,在用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;
            }
        }
    }
  • 相关阅读:
    hadoop 配置
    spark 学习网站和资料
    spark-submit 提交任务及参数说明
    python 浮点运算
    nginx 和 php
    clojure 语法
    编程语言
    spark
    mvn 与 pom.xml
    偏导数与偏微分
  • 原文地址:https://www.cnblogs.com/CWater/p/2109351.html
Copyright © 2011-2022 走看看