简体   繁体   English

params数组可以作为C#中列表的保持类型吗?

[英]Can a params array be a holding type for a list in C#?

Can a params[] variable hold a list, that has been passed as a parameter through a method in C#? params []变量是否可以保存一个列表,该列表已作为参数通过C#中的方法传递?

Wrote a method(AddIntegers) which requires multiple values to be taken as parameters. 写了一个方法(AddIntegers),需要将多个值作为参数。 On the parameter list of a method, there is a params[] variable that is receiving the values thrown, when the method call is made in the Main method. 在方法的参数列表中,当在Main方法中进行方法调用时,有一个params []变量正在接收抛出的值。 But there is an exception that says "Cannot convert from system.collections.generic.List<> to an integer. Anyone knows why does that happen? 但有一个例外,说“无法从system.collections.generic.List <>转换为整数。任何人都知道为什么会发生这种情况?

       **** this is within the Main Method**** 
        string Demostring = Console.ReadLine();
        string[] tokens = Demostring.Split(',');
        List<int> nums = new List<int>();
        int oneNum;
        foreach(string s in tokens)
        {
            if (Int32.TryParse(s, out oneNum))
                nums.Add(oneNum);
        }

        int result1 = AddIntegers(nums);

        **** this is the method to be called****
        public static int AddIntegers(params int[] Restnum)
        {
            int result = 0;

            foreach(int temp in Restnum)
            {
                result += temp;
            }

             return result;
         }

No, a List<int> isn't an int[] , so you can't use it as an argument for an int[] parameter. 不, List<int>不是int[] ,因此您不能将它用作int[]参数的参数。 (The fact that it's a parameter array doesn't change that.) (事实上​​,它是一个参数数组不会改变它。)

Typically the solution to this is to have two overloads - one accepting a parameter array and one accepting an IEnumerable<T> : 通常,解决方案是有两个重载 - 一个接受参数数组,另一个接受IEnumerable<T>

public static int AddIntegers(params int[] numbers) =>
    AddIntegers((IEnumerable<int>) numbers);

public static int AddIntegers(IEnumerable<int> numbers)
{
    ...
}

Note that for this specific example, you can just call the Sum extension method from LINQ to Objects, of course. 请注意,对于此特定示例,您当然可以从LINQ调用Sum扩展方法到Objects。

There are proposals for parameter arrays to be able to accept IReadOnlyList<T> rather than just T[] , but that isn't in C# yet. 有些建议让参数数组能够接受IReadOnlyList<T>而不仅仅是T[] ,但这还不在C#中。 (I'd love it, personally...) (亲爱的,我会爱上它的。)

This is because you are trying to insert a List inside an array parameter. 这是因为您尝试在数组参数中插入List。

Instead of this: 而不是这个:

int result1 = AddIntegers(nums);

You could do this: 你可以这样做:

int result1 = AddIntegers(nums.ToArray());

Or alternatively: 或者:

public static int AddIntegers(List<int> numbers)
{
     return AddIntegers(numbers.ToArray());
}

public static int AddIntegers(params int[] Restnum)
{
    int result = 0;

    foreach(int temp in Restnum)
    {
        result += temp;
    }

    return result;
}

And then call it like you normally would. 然后像平时那样打电话。

private void button4_Click(object sender, EventArgs e)
        {
            const string demostring = "1,2,3,4,5,6";
            var tokens = demostring.Split(',');
            var nums = new List<int>();
            foreach (var s in tokens)
                if (int.TryParse(s, out var oneNum))
                    nums.Add(oneNum);

            var result1 = AddIntegers(nums);
        }
        public static int AddIntegers(List<int> restnum)
        {
            var result = 0;

            foreach (var temp in restnum)
                result += temp;

            return result;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM