c#3.0中一个激动人心的特性就是扩展方法:你可以使用实例方法的语法来调用静态方法。本文仔细阐述了这一新特性并且给出了几个相应的例子。
声明扩展方法
扩展方法的行为和静态方法是非常类似的,你只能在静态类中声明它们。为声明一个扩展方法,你需要给该方法的第一个参数指定this关键字,如下例:
| // program.cs public static class emclass { public static int toint32ext(this string s) { return int32.parse(s); } public static int toint32static(string s) { return int32.parse(s); } } class program { static void main(string[] args) { string s = "9"; int i = s.toint32ext(); // line a console.writeline(i); int j = emclass.toint32static(s); // line b console.writeline(j); console.readline(); } } |
| csc.exe /reference:"c:\program files\linq preview\bin \system.data.dlinq.dll" /reference:c:\windows\microsoft.net\framework\v2.0.50727\system.dll /reference:"c:\program files\linq preview\bin\system.query.dll" /reference:"c:\program files\linq preview\bin\system.xml.xlinq.dll" /target:exe program.cs |
关键字 本文所属关键字
相关 与本文相关文章
分类 所有文章关键字导航源码编程相关Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏 操作系统/服务器相关Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器 标准 网站致力的规范 |