自学内容网 自学内容网

C#_扩展方法

简述:

  • 扩展方法所属类必需是静态类(类名依据规范通常为XXXExtension,XXX为被扩展类)
  • 扩展方法必需是公有静态方法
  • 扩展方法的首个参数由this修饰,参数类型为被扩展类型

示例:

static class DoubleExtension
{
    public static double Round(this double input, int digits)
    {
        double result = Math.Round(input, digits);
        return result;
    }
}

此时可对double类型的变量调用Round方法


原文地址:https://blog.csdn.net/Mudrock__/article/details/136229090

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!