CSharp - Array Initialization Expressions

Introduction

You can omit the new operator and type qualifications:

char[] vowels = {'a','e','i','o','u'};
int[,] rectangularMatrix =
{
       {0,1,2},
       {3,4,5},
       {6,7,8}
};

int[][] jaggedMatrix =
{
       new int[] {0,1,2},
       new int[] {3,4,5},
       new int[] {6,7,8}
};

You can use the var keyword which would let compiler to type a variable.

var rectMatrix = new int[,]    // rectMatrix is implicitly of type int[,]
{
       {0,1,2},
       {3,4,5},
       {6,7,8}
};

var jaggedMat = new int[][]    // jaggedMat is implicitly of type int[][]
{
       new int[] {0,1,2},
       new int[] {3,4,5},
       new int[] {6,7,8}
};

You can omit the type qualifier after the new keyword.

var vowels = new[] {'a','e','i','o','u'};   // Compiler infers char[]

The elements must all be implicitly convertible to a single type

var x = new[] {1,10000000000};   // all convertible to long

1 is inferred to int and 10000000000 is long type.

Quiz