无法将int类型隐式转换为int数组

-4 c# arrays methods int compiler-errors

我有简单的方法.我无法弄清楚它为什么会抛出错误.提前致谢!

 public static int[] Shift(int[] a) 
 {    
       if (a == null) return -1;
       ...
 }
Run Code Online (Sandbox Code Playgroud)

编译器抛出以下错误:

无法隐式转换intint[]

小智 9

如果您的数组为null,则返回int.该函数期望int [].您可以返回null或空数组.

  • @Mario你的意思是null?"null"和空数组之间存在差异.我认为,设计方面,返回null将是更好的选择. (2认同)

Luc*_*uci 5

您正在尝试返回和int但是您的方法需要一组int.在这种情况下你想要回报什么?

你可以返回一个空数组

return new int[]{};
Run Code Online (Sandbox Code Playgroud)

或包含-1的数组

return new int[]{-1};
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不仅仅是一个答案. (3认同)