您的位置:首页 > 编程语言 > C#

C#笔记 -----扩展方法

2014-06-08 01:30 155 查看
在我们使用vs自带的工具函数时,如:

string str='111';
str.toInt();

有没有想到过他们是怎么来的?

这就是C# 的 方法扩展:

age:

using system;

public static class Test

  {

  public static int toInt(this string str)

  {

    try

    {

    Convert.toInt32(str);

    }

  catch(exception ex)

  {

    throw new exception

  }

    }



类似于这种,把类和方法都定义为静态类型,方法的this 代表接受调用对象的类型,就可以实现 前面的扩展方法了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: