Search code examples
c#arraysmethodscompiler-errorsint

Cannot implicitly convert type int to int array


I have simple method. I can't figure out why it throws an error. Thanks in advance!

 public static int[] Shift(int[] a) 
 {    
       if (a == null) return -1;
       ...
 }

Compiler throws the following error:

Cannot implicitly convert type int to int[]


Solution

  • if your array is null you are returning an int. The function is expecting an int[]. You can return null or a blank array.