params参数和正常参数

moh*_*mar -6 .net c# params method-resolution-order

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方法被调用?为什么?

Ham*_*jam 9

在一个简单的句子中" 更具体是比不具体 "

所以 public void foo(int z, int x)会被称为.

这是因为C#中的方法重载解析规则.

你可以在这个答案中阅读更多内容