zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    Understanding String Table Size in HotSpot
    Java性能优化之JVM GC(垃圾回收机制)
    为什么新生代内存需要有两个Survivor区
    jmap命令详解
    JVM GC 机制与性能优化
    JVM1.6 GC详解
    jstat命令详解
    锁的性能相关
    JAVA AQS源码分析
    kafka的安装 (单机)
  • 原文地址:https://www.cnblogs.com/itmu89/p/7054022.html
Copyright © 2011-2022 走看看