在 OOPL 中,有静态方法、实例方法和虚方法,如下:
 
public sealed class String
{
     public static bool  IsNullOrEmpty(string s)
     {
          // ...
     }
 
     public string Replace(string old, string new)
     {
          // ...
     }
}
public abstract class Stream
{
     public virtual void WriteByte(byte value)
     {
          // ...
     }
}
 
其中,IsNullOrEmpty 是静态方法,Replace 是实例方法,而 WriteByte 就是虚方法。其用法如下:
 
String name = null;
Console.WriteLine(String.IsNullOrEmpty(name));      // true
 
String s = "Hello, world!";
Console.WriteLine(s.Replace("world", "C#"));          // Hello, C#!
 
虚方法需要在导出类(derived class)中重新定义,此处不赘。
 
而在 C# 3.0 中,又引入了一种方法,称为 扩展方法(Extension Method):
 

Extension methods allow an exsting type to be extended with new methods without altering the definition of the original type.

扩展方法是用新方法来扩展原有类型,而不用修改原有类型的定义。
 
扩展原有类型(Extending the existing type)
 
扩展原有类型的机制(手段)有类的继承,还有 C# 中的扩展方法。
 
类的继承通过增加新成员,可以扩展原有类型的数据成员(fields);可以定义新方法或重写被继承类的虚方法来扩展原有类型的方法成员(methods)。但继承机制也存在自己的问题,如果原有类型声明为封闭类型(sealed class),则无法使用继承机制扩展原有类型(如上述的 String class)。再者,导出类(derived class)对基类(base class)的扩展是作用在导出类上的,而非对基类自身的扩展。
 
扩展方法弥补了继承机制的不足。严格说来,扩展方法不是语言本身的机制,而是通过编译程序实现的语法便用设施。不管原有类型是封闭类型还是开放类型,都可以用扩展方法进行扩展。扩展方法只是扩展原有类的方法成员,并不能扩展原有类的数据成员。
 
An extension method is a static method of a static class, where the this modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
扩展方法是静态类的静态方法,其第一个参数要用 this 修饰,而这第一个参数的类型就是要扩展的类型。
 
例子:
 
public static class StringHelper
{
     public static bool IsCapitalized(this string s)
     {
          if (string.IsNullOrEmpty(s)) return false;
          return char.IsUpper(s[0]);
     }
}
 
IsCapitalized 就是定义在静态类上的静态方法,而其第一个参数 string s 用 this 修饰,所以 IsCapitalized 是扩展方法,因为第一个参数的类型是 string,所以 IsCapitalized 扩展的是 string 类型。
 
用法:
 
Console.WriteLine("Perth".IsCapitalized());
 
"Perth".IsCapitalized() 是直接针对 string "Perth" 调用 IsCapitalized(),因为 IsCapitalized() 是 string 的扩展方法,所以这种调用是可以的。
 
扩展方法的调用经过编译器的解析后,就翻译为普通的静态方法调用,即:
 
Console.WriteLine(StringHelper.IsCapitalized("Perth"));
 
而 "Perth".IsCapitalized() 相对于 StringHelper.IsCapitalized("Perth") 而言,更为自然、简洁。
 
所以,扩展方法仍然是一种静态方法,只是 C# 语言引入的语法便用设施而已。编译器的翻译过程如下:
 
     arg0.Method(arg1, arg2, ...);  ==> StaticClass.Method(arg0, arg1, arg2, ...);
 
也就是将扩展方法调用翻译为普通的静态方法调用。
 
扩展方法的应用
 
string 类虽然提供了功能强大的众多方法,但总是不可能满足所有需求。有些经常使用而 string 类又没有提供的功能,可以写成扩展方法,并归入代码库(Library),可以再多个项目中使用。下面是一个例子,两个有关 string 的扩展方法,放入 StringExtensions 类中:
 
    public static class StringExtensions
    {
        public static List<KeyValuePair<string, string>> Listize(this string ss)
        {
            List<KeyValuePair<string, string>> pList = new List<KeyValuePair<string, string>>();
            string[] slist = ss.Split(new char[] { ';' });
            foreach (string s in slist)
            {
                if (string.IsNullOrEmpty(s)) continue;
                string[] pair = s.Split(new char[] { ':' });
                if (pair.Length != 2) continue;
                pList.Add(new KeyValuePair<string, string>(pair[0], pair[1]));
            }
            return pList;
        }
        public static string MatchAndReplace(this string src, string pattern, string dst)
        {
            List<string> mList = new List<string>();
            foreach (Match m in Regex.Matches(src, pattern))
            {
                mList.Add(m.Value);
            }
            foreach (string s in mList)
            {
                if (string.IsNullOrEmpty(s)) continue;
                src = src.Replace(s, dst);
            }
            return src;
        }
    }
 
上述两个扩展方法的用例如下:
 
    string sql = GetSQL();
    string whereTail = "";
    foreach (string k in po.CustomObject.Keys)
    {
        if (string.IsNullOrEmpty(k)) continue;
        if (string.Compare(k, "DemoZoneList", true) == 0)
        {
            whereTail = po.CustomObject[k].Value.Listize().BuildWhereClauseInclusive();
        }
        else if (string.Compare(k, "NonDemoZoneList", true) == 0)
        {
            whereTail = po.CustomObject[k].Value.Listize().BuildWhereClauseExclusive();
        }
    }
    if (!string.IsNullOrEmpty(whereTail))
    {
        sql = sql.MatchAndReplace("1[ ]*=[ ]*1", whereTail);
    }
 
可以看到,string 的扩展方法就像原生方法(指 String 类定义的方法)一样调用,代码看起来更加自然、简洁。
 
扩展方法机制是为了解决 LINQ 的问题而引入的,关于这一点,当另文别述。关于扩展方法的深入讨论,可以参阅《C# in Depth》 Third Edition。
 
 

Extension Methods(扩展方法)的更多相关文章

  1. [C#] Extension Method 扩展方法

    当我们引用第三方的DLL.或者Visual Studio自己的库的时候,或许会发现这样的一个情况,如果这个类型有一个XX的方法就好了.这时候我们可以用到扩展方法,是我们的代码更加灵活和高效. 这里我举 ...

  2. C# Extension Methods(C#类方法扩展)

    使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...

  3. C# -- 扩展方法的应用(Extension Methods)

    当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...

  4. (转)C# -- 扩展方法的应用(Extension Methods)

    本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...

  5. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  6. C# Note21: 扩展方法(Extension Method)及其应用

    前言 今天在开会时提到的一个概念,入职3个多月多注重在项目中使用C#的编程知识,一直没有很认真地过一遍C#的全部语法,当我们新人被问及是否了解Extension Method时,一时之间竟不能很通俗准 ...

  7. [译文]c#扩展方法(Extension Method In C#)

    原文链接: https://www.codeproject.com/Tips/709310/Extension-Method-In-Csharp 介绍 扩展方法是C# 3.0引入的新特性.扩展方法使你 ...

  8. [0] C# 扩展方法(Extension Method)

    有时有这样的情况,有一个类,你不能修改它,但你又想对它扩展(添加一个方法),这个时候就可以用到扩展方法了.请看下面的例子: using System;using System.Collections. ...

  9. c#编程指南(五) 扩展方法(Extension Method)

    C# 3.0就引入的新特性,扩展方法可以很大的增加你代码的优美度,扩展方法提供你扩展.NET Framewoke类的扩展途径,书写和规则也简单的要命. 编写扩展方法有下面几个要求: 第一:扩展方法所在 ...

随机推荐

  1. redis—持久化

    Redis持久化 概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内 ...

  2. 使用 XMLHttpRequest实现Ajax

    [XMLHttpRequest的概述] 1.XMLHttpRequest最早是在IE5中以ActiveX组件的形式实现的.非W3C标准 2.创建XMLHttpRequest对象(由于非标准所以实现方法 ...

  3. noip模拟赛 水管工的难题

    [问题描述]你是一名优秀的水管工. 一天你遇到了一个棘手的难题. 你需要在一个长方体状的房间内连接一条贯穿房间内部的水管.房间的长为 X,宽为 Y,高为 Z, 整个房间可以看成是 X×Y×Z个小立方体 ...

  4. Wikioi 3776 生活大爆炸版石头剪子布

    题目描述 Description 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8集中出现了一种石头剪刀布的升级版游戏 ...

  5. 初识iBatis

    在JAVA EE应用程序中,持久层框架常用的有:Hibernate和IBATIS(或MyBatis),Hibernate是全自动的,IBatis是半自动的. IBatis的主要的作用是把SQL语句从我 ...

  6. Bestcoder #92

    A =w= B 计数题,枚举A.C,算B.D的个数,注意减去重复的 我当时是f[1][n]->f[2][n]->f[3][n]->f[4][n]递推的 C 题意:长为n的字符串仅由' ...

  7. Linux: 查找使用中的port

    找出所有使用中的port netstat -tulpn 输出 Active Internet connections (only servers) Proto Recv-Q Send-Q Local ...

  8. 【Android实战】记录自学自己定义GifView过程,能同一时候支持gif和其它图片!【有用篇】

    之前写了一篇博客.<[Android实战]记录自学自己定义GifView过程,具体解释属性那些事! [学习篇]> 关于自己定义GifView的,具体解说了学习过程及遇到的一些类的解释,然后 ...

  9. leetCode(49):Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 推断一个数是否是质数主要有下面几种 ...

  10. 在项目开发中使用Git版本号控制工具以提高效率

    安装Git(linux centos平台) 源代码方式安装 1.装依赖 $ yum install curl-devel expat-devel gettext-devel openssl-devel ...