(73)C# 扩展方法

时间:2020-04-30 21:06:23   收藏:0   阅读:67

 

扩展方法的要求:

1.扩展方法要求在一个静态类中

2.扩展方法本身也是静态方法

3.扩展方法第一个参数是    [this 要扩展的类 参数名称]

    public class Program
    {
        static void Main()
        {
            int a = 10;
            int b=a.fun();
            Console.WriteLine(b);
        }
    }

    public static class A
    {
        public static int fun(this int num)
        {
            return num * 2;
        }
    }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!