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的原因 查阅了一下资料, ...
随机推荐
- 打开已存在 Android项目及常见的问题
Eclipse 打开已存在 Android项目及常见的问题 1. 点击菜单“File”-- "Import",会弹出 Import 对话框: 2, 选择“General ...
- android学习日记13--数据存储之SQLite
2.SQLite 开源轻量级数据库,支持92-SQL标准,主要用于嵌入式系统,只占几百K系统资源此外,SQLite 不支持一些标准的 SQL 功能,特别是外键约束(FOREIGN KEY constr ...
- 性能测试中用LambdaProbe监控Tomcat Tomcat和Probe的配置
转载:http://bbs.51testing.com/thread-90047-1-1.html 性能测试中用LambdaProbe监控TomcatLambdaProbe 是一款强大的免费开源工具, ...
- C#_dropdownlist_3
DropDownList则与TextBox等控件不同,它使用的是select标记.它需要两个值:在下拉框中显示的列表,和默认选项.而自动绑定一次只能绑定一个属性,因此你需要根据需要选择是绑定列表,还是 ...
- java_spring_依赖注入(构造器)
依赖注入对象可以 手工装配(建议) 和 自动装配 package com.PersonDaoBean.test; public interface PersonDao { public abstrac ...
- xss脚本攻击
xss脚本攻击不仅仅只是alert(1)就算完了,xss脚本攻击真正的用处是盗取普通用户的cookie,或者盗取管理员的cookie. xss分类(类型): 1. 反射型xss2. 存储型xss3. ...
- 16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
EOBD2.FR is offering 16% discount off the latest MPPS V16 ECU chip tuning tool. The device is now so ...
- CSS——选择器
css选择器 css选择器可分为:标签(元素)选择器,ID选择器,类选择器,属性选择器,后代选择器,子代选择器,相邻兄弟选择器和兄弟选择器.... 标签选择器: //E{attr:value;attr ...
- 一段JavaScript代码
eval(function(p, a, c, k, e, d) { e = function(c) { return c.toString(36) }; if (!''.replace(/^/, St ...
- Xilinx 网站资源导读2
Xilinx 网站资源导读 ———版权声明———–本文作者 Ricky Suwww.fpganotes.comrickysu.fpga@gmail.com 欢迎转载,转载请保持原样及署名商业使用须得到 ...