(73)C# 扩展方法

 

扩展方法的要求:

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;
        }
    }

 

posted @ 2020-04-30 20:21  富坚老贼  阅读(121)  评论(0)    收藏  举报