扩展其实真的很简单

msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

扩展方法的关键不在于定义所在的class的名字,关键在于扩展方法的第一个参数,以及所有class和扩展方法是否为static。

扩展方法的第一个参数指定这个扩展方法作用在的class。

比如我的例子中为string添加一个扩展方法 (ToIntExt), 所以第一个方法必须是string类型,并且要加上this修饰符为前缀。

完成了这些工作,你在StringExtension的作用域里任何string类型的实例都可以访问这个扩展方法(如上图)。

 

扩展方法还可以添加参数,想要实现只需在第一个参数(this前缀)后面继续添加你需要的参数。如下,我想给我的ToIntExt扩展方法加一个format:

Extension method for type的更多相关文章

  1. 动态linq表达式新方法,Dynamic LINQ Extension Method

    Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its impleme ...

  2. Extension Method[下篇]

    四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...

  3. Extension Method[上篇]

    在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...

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

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

  5. C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument

    &apos;System.Collections.Generic.IEnumerable<string>&apos; does not contain a definiti ...

  6. Swift protocol extension method is called instead of method implemented in subclass

    Swift protocol extension method is called instead of method implemented in subclass protocol MyProto ...

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

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

  8. The method of type must override a superclass method

    导入android项目时,报The method of type must override asuperclass method 一堆错误, 解决方法: 将编译的jdk与使用的jdk版本一致即可.

  9. The method of type must override a superclass method解决方式

    工程导入myeclipse时,出现问题提示:The method of type must override asuperclass? annotation:@Override的原因 查阅了一下资料, ...

随机推荐

  1. careercup-中等难题

    17.1 编写一个函数,不用临时变量,直接交换两函数. 解法: 方法一:这个是经典面试题,也相当直接.我们将用a0表示a的初值,b0表示b的初始值,用diff表示a0-b0的值. 让我们将a>b ...

  2. C如何获取文件夹下所有文件

    http://baike.baidu.com/view/1186290.htm?fr=aladdin 使用io.h中的_findfirst,_findnext,_findclose,_finddata ...

  3. java_JdbcUtilis_单实例

    //eg1,没有使用单实例,eg2有 package cn.itcast; import java.sql.Connection; import java.sql.DriverManager; imp ...

  4. Mechanism of Loading Resources

    Mechanism of Loading Resources 1. Distributed strategy 1.1. Developer guilde 1.2. Notes 2. Centraliz ...

  5. Android 快速开发框架网络篇-Android-Async-Http

    一.基本用法 AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", ...

  6. java与c++的访问权限的问题

    转自网络:http://blog.sina.com.cn/s/blog_4bc954d30100wtxe.html 权限的理解重要理解什么是可见什么是不可见 首先声明:java中,friendly这个 ...

  7. jsp The requested resource (/demo10/loginBean) is not available.

    The requested resource (/demo10/loginBean) is not available. <?xml version="1.0" encodi ...

  8. Android_Handler

    xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  9. The Shapes of CSS

    #square { width: 100px; height: 100px; background: red; } #rectangle { width: 200px; height: 100px; ...

  10. js观察者模式

    观察者模式存在观察者和被观察者 被观察者的状态发生改变,通知观察者调用观察者的update方法,观察者的update方法对被观察者的状态进行检测,做出相应的操作 被观察者存在接口attach,deta ...