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;
            }
        }
  • 相关阅读:
    利用Dom4j创建xml文档
    eclipse安装、使用hibernate插件方法
    “万能数据库查询分析器”用户已基本涵盖当前所有DBMS
    在Dom4j中使用Xpath搜索xml的元素节点
    多线程的一些小问题集锦
    Dalvik虚拟机的运行过程分析
    创建线程的两种方式
    通过xpath查找指定的节点
    Oracle TNS 配置
    c++五种内存分配、堆与栈区别
  • 原文地址:https://www.cnblogs.com/itmu89/p/7054022.html
Copyright © 2011-2022 走看看