zoukankan      html  css  js  c++  java
  • How to extend IEnumable<T> ?

    Here is a example:

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

    namespace LamdaExpressionTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var list = new List<Product>
                {
                    new Product{
                        ProductID = 1,
                        ProductName = "aaaa",
                        Description = "this is aaaa"
                    },
                    new Product{
                        ProductID = 2,
                        ProductName = "bbbb",
                        Description = "this is bbbb"
                    },
                    new Product{
                        ProductID = 3,
                        ProductName = "cccc",
                        Description = "this is cccc"
                    },
                    new Product{
                        ProductID = 4,
                        ProductName = "dddd",
                        Description = "this is dddd"
                    },
                    new Product{
                        ProductID = 5,
                        ProductName = "eeee",
                        Description = "this is eeee"
                    },
                    new Product{
                        ProductID = 6,
                        ProductName = "ffff",
                        Description = "this is ffff"
                    },
                };

                Func<Product, bool> fun = (Product product) =>
                {
                    return product.ProductID % 2 == 0;
                };
                var list2 = list.Where(fun).ToList();
                var list3 = list.FilterByHashCode().ToList();

                list.ForEach(item => Console.WriteLine(item.ProductName));
            }
        }

        public class Product
        {
            public int ProductID { setget; }
            public string ProductName { setget; }
            public string Description { setget; }
        }

        public static class IEnumableExtension
        {
            public static IEnumerable<T> FilterByHashCode<T>(this IEnumerable<T> list)
            {
                if (list == null || list.Count() < 1)
                {
                    yield break;
                }

                foreach (var item in list)
                {
                    if (item.GetHashCode() % 2 == 0)
                    {
                        yield return item;
                    }
                }
            }

            public static void Foreach<T>(this IEnumerable<T> colection, Action<T> action)
            {
                if(colection == null || colection.Count() < 1)
                    return;

                foreach(var item in colection)
                {
                    action(item);
                }
            }
        }


  • 相关阅读:
    1010 Radix (25 分)(二分)【回顾】
    1089 Insert or Merge (25 分)(two pointers)【回顾】
    1084 Broken Keyboard (20 分)(字符串处理)
    Listener监听器之HttpSessionListener
    在IE中如何在用户直接关闭窗口前清除Session
    C#文件操作简单封装
    几个实用的对String类的扩展
    C# 数据加密解密
    在IIS6中配置asp.net MVC网站时HTTP 错误 500.21 Internal Server Error解决方案
    C#常用的正则表达式
  • 原文地址:https://www.cnblogs.com/Langzi127/p/2236468.html
Copyright © 2011-2022 走看看