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;
            }
        }
    }
  • 相关阅读:
    Nginx日志管理
    Nginx负载均衡
    Nginx 缓存
    Nginx代理服务器
    Nginx搭建 Web服务
    Nginx HTTP模块
    洛谷P1012拼数
    洛谷 P1876 开灯
    洛谷P2084 进制转化
    关于typedef的用法
  • 原文地址:https://www.cnblogs.com/CWater/p/2109351.html
Copyright © 2011-2022 走看看