[英]In C# How to correctly pass an int array to a function
how do I pass an array with an unknown number of integers to a function?如何将具有未知数量整数的数组传递给 function? Can anybody tell me what I am doing wrong?
谁能告诉我我做错了什么?
I get the following error when trying to run the code:尝试运行代码时出现以下错误:
Error CS1501 No overload for method 'Solution' takes 6 arguments错误 CS1501 方法“解决方案”没有重载需要 6 arguments
using System;
namespace IntegerTest
{
class Program
{
public static int Solution(int[] input)
{
Array.Sort(input);
int index = 0;
// Skip negatives
while (index < input.Length && input[index] < 1)
index++;
int expected = 1;
while (index < input.Length)
{
if (input[index] > expected)
return expected;
// Skip number and all duplicates
while (index < input.Length && input[index] == expected)
index++;
expected++;
}
return expected;
}
public static void Main()
{
Console.WriteLine(Solution( 1, 3, 6, 4, 1, 2));
}
}
}
You can either call the function with an array argument (eg Solution(new[] {1, 3, 6, 4, 1, 2})
, or modify the function signature to take a params
argument ( int Solution(params int[] input)
).您可以使用数组参数(例如
Solution(new[] {1, 3, 6, 4, 1, 2})
调用 function ,或修改 function 签名以采用params
参数( int Solution(params int[] input)
)。
Your method accepts a int[]
, so create a new int[]
你的方法接受一个
int[]
,所以创建一个new int[]
Solution(new int[] {1, 3, 6, 4, 1, 2});
You are passing 6 arguments to a method that takes one.您将 6 arguments 传递给一个需要一个的方法。 Change your main method to something like this:
将您的主要方法更改为以下内容:
public static void Main()
{
int[] arr = { 1, 3, 6, 4, 1, 2};
Console.WriteLine(Solution(arr));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.