0

This method does not compile with the error "Default parameter for Index1 must be a compile-time constant. I am passing a int[3]; Why is this happening? How can I solve this?

    public void C_Loader(int[] Index1 = new int[3] {4,4,4}, int[] Index2 = new int[3] {8,8,8}, int[] Index3 = new int[3] {10,10,10})
Roblogic
  • 107
  • 3
  • 8
  • Same question has been asked and answered [here](https://stackoverflow.com/questions/12607146/method-parameter-array-default-value) – sharp Jan 17 '22 at 11:49

1 Answers1

2

From MSDN

"the default value must be one of the following :

a constant expression;

an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;

an expression of the form default(ValType), where ValType is a value type."

Arrays you created don't follow any of the above rulles

try this

public void C_Loader(int[] Index1=null, int[] Index2=null , int[] Index3=null)
 {
    if(Index1 ==null) Index1= new int[] {4,4,4};
    if (Index2 == null) Index2 = new int[] { 8, 8, 8 };
    if (Index3 == null) Index3 = new int[] { 10, 10, 10 };


        .... your code
 }
Serge
  • 40,935
  • 4
  • 18
  • 45