什么是重载

重载条件:

在同一个类中,同名,返回类型可以不同,参数列表不能相同(参数个数和参数类型),访问修饰符可以不同,最重要的是要同名,具有不同的参数,无论返回值、访问修饰符是否相同,方法名必须相同,参数列表必须不参相同。
如下类中的重载方法

 class D
    {
        public int test(int n)
        {
            return n;
        }
        internal string test(string str)
        {
            return str;
        }
    }

下面类中的两个方法参数列表相同,因此重载失败,有编译错误

 class D
    {
        public int test()
        {
            return 12;
        }
        internal string test()
        {
            return "你好!";
        }
    }

下面类中的两个方法参数列表虽然不同,但仅是因为ref和out关键字而导致的不同,因此也不是重载,有编译错误

class D
    {
        public int test(ref int n)
        {
            return n;
        }
        internal int test(out int n)
        {
            return n;
        }
    }

原文地址:https://www.cnblogs.com/liancs/p/3879364.html