自定义扩展方法(集合泛型约束)超好用,这里理解方法当参数来使用

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

namespace ConsoleApplication17
{
    public static class STSD
    {
        public static List<T> MyWhere<T>(this List<T> list, Func<T, bool> funWhere)
        {
            List<T> mList=new List<T>();
            foreach (T r in list)
            {
                if (funWhere(r))
                {
                    mList.Add(r);
                }
            }
            return mList;
           
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<string> strList=new List<string>()
            {
                "3","9","32","7"

            };
          strList= strList.MyWhere<string>(a=>a.CompareTo("6")<0).ToList();
            //strList = strList.Where(a=> Convert.ToInt32(a.ToCharArray()[0].ToString()) <6).ToList();
            //strList = strList.Where(a => { return Convert.ToInt32(a.ToCharArray()[0].ToString()) <6; }) .ToList();
            strList.ForEach(a => { Console.WriteLine(a);});
            Console.ReadKey();

        }
    }
}

posted on 2015-05-03 17:06  听哥哥的话  阅读(219)  评论(0编辑  收藏  举报

导航