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

  在我们使用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 代表接受调用对象的类型,就可以实现 前面的扩展方法了

原文地址:https://www.cnblogs.com/mr-jia/p/3775671.html