What is difference between these two array declarations?

前端 未结 5 1971
自闭症患者
自闭症患者 2021-01-11 21:05

It seems these two declarations are the same:

int[] array1 = {11, 22, 33};

and

int[] array2 = new int[] {11, 22, 33};
         


        
5条回答
  •  伪装坚强ぢ
    2021-01-11 21:45

    int[] array1; // declare array1 as an int array of any size
    array1 = new int[10];  // array1 is a 10-element array
    array1 = new int[20];  // now it's a 20-element array
    
    int[] array2 = new int[5] {1, 2, 3, 4, 5}; // declare array 2 as an int array of size 5
    

    Both are the same. See here at MSDN on initializing arrays.

提交回复
热议问题