Arrays. What is correct?

Hi,
why sometimes need to declare number of arrays places and other times not?
What is correct?

var arr = new int[3] {1,2,3};
or
var arr2 = new int[] {1,2,3};

Both are correct. You need to supply the array size if the compiler can’t infer it. If you provide an array initializer the compiler can infer the size from the number of values you provide there. If you don’t provide an initializer you need to specify the size explicitly.

2 Likes