C#扩展方法

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。

扩展方法三个要素
1.静态类
2.静态方法
3.this关键字
 
 
 static void Main(string[] args)
        {
            List<string> strlist = new List<string>() {"12","25","24","10","50","60","1"};
            var temp = strlist.MyWhere(delegate(string a) { return a.CompareTo("25") < 0; });
            foreach (var item in temp)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
 
 
 
  public static class MyListExt  //静态类
    {
        public static List<string> MyWhere(this List<string> list, Func<string, bool> funcWhere)  //静态方法  //this关键字
        {
            List<string> result = new List<string>();
            foreach (var item in list)
            {
                if (funcWhere(item))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
原文地址:https://www.cnblogs.com/itmu89/p/7054022.html