C#_扩展方法

简述:

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

示例:

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

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

相关推荐
nnsix9 小时前
C# 字符串 根据换行符分割
开发语言·c#
吴可可1239 小时前
C# Exception命名空间冲突解决方案
c#
佚泽12 小时前
C# webApi学习笔记
笔记·学习·c#
魔法阵维护师12 小时前
从零开发游戏需要学习的c#模块,第二十四章(瓦片地图 —— 让世界有墙)
学习·游戏·c#
吴可可12312 小时前
C#中括号报错“应输入标识符”原因解析
c#
lingxiao1688814 小时前
智慧停车场(SmartParking)
c#·自动化·wpf
yngsqq15 小时前
加载dll失败
c#
吴可可12318 小时前
C#中is运算符的正确用法
c#