小编moh*_*mar的帖子

使用 params 关键字重载方法

class D
{
    public void foo(int z, int x)
    {
        Console.WriteLine("foo with two parameters");
    }
    public void foo(params int[] z)
    {
        Console.WriteLine("foo with two params parameter");
    }
}
class Program
{
    public static void Main()
    {       
        D obj = new D();
        obj.foo(10,20);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,将执行该方法foo(int x, int y)而不是foo(params int[] z). 为什么是这样?

c# parameters

1
推荐指数
1
解决办法
2003
查看次数

params参数和正常参数

using System;
using System.Collections.Generic;

namespace Generics
{
    class Minivan
    {
        public void foo(int z, int x)
        {
            Console.WriteLine("foo with two parameters");
        }
        public void foo(params int[] z)
        {
            Console.WriteLine("foo with two params parameter");
        }
    }
    class D
    {
        public static void Main()
        {
            Minivan car3 = new Minivan();
            car3.foo(10,20); // which method will be called here!!!
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个foo方法被调用?为什么?

.net c# params method-resolution-order

-6
推荐指数
1
解决办法
87
查看次数

标签 统计

c# ×2

.net ×1

method-resolution-order ×1

parameters ×1

params ×1