简体   繁体   中英

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#?

Wrote a method(AddIntegers) which requires multiple values to be taken as parameters. 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. But there is an exception that says "Cannot convert from system.collections.generic.List<> to an integer. Anyone knows why does that happen?

       **** 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. (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> :

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.

There are proposals for parameter arrays to be able to accept IReadOnlyList<T> rather than just T[] , but that isn't in C# yet. (I'd love it, personally...)

This is because you are trying to insert a List inside an array parameter.

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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