c#函数重载

 

转:http://blog.csdn.net/jianzaochuanwu/article/details/7587913

c#函数重载

分类: c#编程 28人阅读 评论(0) 收藏 举报

c#方法重载:一个类中可以有一个以上名称相同的方法。但它们的签名不同,也就是括号里面的参数不同。仅方法返回值不同,是不能重载的。也就是方法重载与返回值无关。

常用的两种重载:

        public static int compute(int x, int y)
        {
            return x + y;
        }
        //方法参数个数不同
        public static int compute(int x, int y, int z)
        {
            return x + y + z;
        }
        //方法参数类型不同
        public static double compute(double x, double y)
        {
            return x + y;
        }

调用上面的方法:

        static void Main(string[] args)
        {
            Console.WriteLine(compute(3, 2));
            Console.WriteLine(compute(3.1,2.1));
            Console.WriteLine(compute(3, 2, 1));
            Console.ReadKey();
        }

输出结果:5,5.2,6

posted on 2013-12-25 09:03  yeren  阅读(204)  评论(0编辑  收藏  举报