Extension method for type
扩展其实真的很简单
msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。”
扩展方法的关键不在于定义所在的class的名字,关键在于扩展方法的第一个参数,以及所有class和扩展方法是否为static。

扩展方法的第一个参数指定这个扩展方法作用在的class。
比如我的例子中为string添加一个扩展方法 (ToIntExt), 所以第一个方法必须是string类型,并且要加上this修饰符为前缀。
完成了这些工作,你在StringExtension的作用域里任何string类型的实例都可以访问这个扩展方法(如上图)。
扩展方法还可以添加参数,想要实现只需在第一个参数(this前缀)后面继续添加你需要的参数。如下,我想给我的ToIntExt扩展方法加一个format:

Extension method for type的更多相关文章
- 动态linq表达式新方法,Dynamic LINQ Extension Method
Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its impleme ...
- Extension Method[下篇]
四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...
- Extension Method[上篇]
在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...
- C# Note21: 扩展方法(Extension Method)及其应用
前言 今天在开会时提到的一个概念,入职3个多月多注重在项目中使用C#的编程知识,一直没有很认真地过一遍C#的全部语法,当我们新人被问及是否了解Extension Method时,一时之间竟不能很通俗准 ...
- C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument
'System.Collections.Generic.IEnumerable<string>' does not contain a definiti ...
- 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 ...
- [C#] Extension Method 扩展方法
当我们引用第三方的DLL.或者Visual Studio自己的库的时候,或许会发现这样的一个情况,如果这个类型有一个XX的方法就好了.这时候我们可以用到扩展方法,是我们的代码更加灵活和高效. 这里我举 ...
- The method of type must override a superclass method
导入android项目时,报The method of type must override asuperclass method 一堆错误, 解决方法: 将编译的jdk与使用的jdk版本一致即可.
- The method of type must override a superclass method解决方式
工程导入myeclipse时,出现问题提示:The method of type must override asuperclass? annotation:@Override的原因 查阅了一下资料, ...
随机推荐
- 文件I/O(不带缓冲)之write函数
调用write函数向打开的文件写数据. #include <unistd.h> ssize_t write( int filedes, const void *buf, size_t nb ...
- rem移动端适配
rem作为一个低调的长度单位,由于手机端网页的兴起,在屏幕适配中得到重用.使用 rem 前端开发者可以很方便的在各种屏幕尺寸下,通过等比缩放的方式达到设计图要求的效果. rem 的官方定义『The f ...
- NAT技术基本原理与应用
转载自:http://www.cnblogs.com/derrick/p/4052401.html?utm_source=tuicool&utm_medium=referral#undefin ...
- AppUse学习笔记
AppUse学习笔记http://www.docin.com/p-786350740.html
- debian 颜色设置
root@debian:/# vi /root/.bashrc #export PS1='\h:\w\$ 'export PS1='\[\033[1;32;40m\]\u@\h:\w\$ \[\033 ...
- win7 X64可用的单文件IE6
由于日常调试需要用到老版本的ie,没有办法!用ietest老师感觉不好使,动不动就死了.就找到了ie的单文件版,有博主 小明爱切糕制作,IE6.7.8单文件版本 http://www.cnblogs. ...
- oracle拼接字段用||
① //dual相当于一个临时表.用来测量@@@@H210000000003I4R 的长度用length() select length('@@@@H210000000003I4R') from du ...
- Could not find artifact com.sun:tools:jar:1.5.0解决方法
可以参照在XP系统下搭建maven环境出的问题 Unable to locate the Javac Compiler in: C:\Program Files\Java\jre6\..\lib\to ...
- Android Metro风格的Launcher开发系列第二篇
前言: 各位小伙伴们请原谅我隔了这么久才开始写这一系列的第二篇博客,没办法忙新产品发布,好了废话不说了,先回顾一下:在我的上一篇博客http://www.cnblogs.com/2010wuhao/p ...
- SVN-钩子
先说说钩子是干什么的吧,,简单的说,svn钩子就是在提交svn时前后所要触发的事件,于是我们可以用钩子做一些提交时的限制,及提交后的操作.最常用的一般有两个,pre-commit,post-commi ...