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);
                }
            }
        }


  • 相关阅读:
    数据结构(2)链表的实现
    vc 调试方法-2
    c语法拾遗-关于指针变量的声明
    收集的一些无聊的网站
    《将博客搬至CSDN》的文章
    黑马程序员-面向对象
    黑马程序员-类加载机制和反射。
    黑马程序员- 正则表达式
    黑马程序员-网络编程
    黑马程序员-File类+递归的简单应用
  • 原文地址:https://www.cnblogs.com/Langzi127/p/2236468.html
Copyright © 2011-2022 走看看