扩展方法
2015-08-27 23:18
183 查看
扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀
定义静态类中的静态(扩展)方法:
使用上述扩展方法:
运行:
定义静态类中的静态(扩展)方法:
public static class Extension { public static int ToInt(this string str) { int val; int.TryParse(str, out val);//这里当转换失败时返回的val为0 return val; } }
使用上述扩展方法:
static void Main(string[] args) { string str = "23"; int i = str.ToInt(); Console.WriteLine(i); Console.ReadKey(); }
运行:
相关文章推荐
- Linux splint命令
- Activity的启动模式
- linux下的 fork vfork和clone函数
- 敏捷软件开发:原则、模式与实践——第11章 DIP:依赖倒置原则
- STM32 UART DMA实现未知数据长度接收
- iOS GCD多线程介绍
- openssl从PFX导出私钥、公钥
- do...while(0)的妙用
- 一个C语言典型的内存泄露问题
- Backdoor.Win32.Rbot病毒防治
- OC-@property增强使用
- haproxy根据客户端浏览器进行跳转
- Java基础の第一弹
- UIViewController
- 一些简单排序算法的下界
- 安装minGW编译器及其使用方法
- springMVC上传文件
- CF 500D New Year Santa Network tree 期望 好题
- [MFC] MDI框架下 vs2010 动态添加菜单
- Android OnTouchListener触屏事件接口