C# Array Initializer Examples

Array Initializers. Many forms of array initializer syntax are available. They tell the C# compiler what you want to place in a new array. The C# compiler infers the length of the array to create from the source text. It infers too the number of dimensions.

Example. An array initializer in the C# language uses the { } curly brackets with elements in comma-separated lists. The length of the array to be created is inferred from the number of elements specified in the source code.

Also: The number of ranks (dimensions) in the arrays is determined. You do not need to specify the length of the arrays.

Array Length
C# program that uses array initializers

using System;

class Program
    static void Main()
	// You can declare arrays with curly brackets.
	// ... It is okay to omit the type on the expression.
	int[] array1 = { 1, 2, 3 };
	int[] array2 = new int[] { 1, 2, 3 };
	// Shows array initializations with strings.
	// ... You can specify two-dimensional arrays.
	// ... You can use empty arrays.
	string[] array3 = { "dot", "net", "perls" };
	string[] array4 = new string[] { "DOT", "NET", "PERLS", null };
	string[,] array5 = { { "dot", "perls" },
			   { "framework", "4.0" } };
	string[] array6 = { };
	// Print out the length and ranks of the arrays.
	Console.WriteLine("array1: {0} length", array1.Length);
	Console.WriteLine("array2: {0} length", array2.Length);
	Console.WriteLine("array3: {0} length", array3.Length);
	Console.WriteLine("array4: {0} length", array4.Length);
	Console.WriteLine("array5: {0} length, {1} rank", array5.Length, array5.Rank);
	Console.WriteLine("array6: {0} length, {1} rank", array6.Length, array6.Rank);


array1: 3 length
array2: 3 length
array3: 3 length
array4: 4 length
array5: 4 length, 2 rank
array6: 0 length, 1 rank

This program shows the array initializer syntax form in a statement for a one-dimensional int[] array. The first two array initializers, which create a three-integer integer array, are precisely equivalent.

Int Array

Note: This means they compile to the same instructions to populate the array. The "new int[]" syntax part is not required.

Next, the program creates a two-dimensional array using the curly bracket array initializer syntax form. The string[,] array with identifier array5 creates a two-dimensional arrays with two columns and two rows.

2D Array

So: The lengths are inferred from the initializers. The number of dimensions, which are also called ranks, are also inferred.

Discussion. The C# language specification describes array initializers as being precisely equivalent to other syntaxes. We see that an array initializer is converted to a sequence of assignments into the newly-allocated arrays.

So: For this reason, performance of array initializers is equivalent in programs. The only difference is the form of the source text.

Tip: Please see chapter 12 in The C# Programming Language Specification third edition.

Summary. The C# compiler can understand and infer array creations. The dimensions and the length of the arrays is inferred from the initializers. The number of elements specified in the curly brackets is used to generate instructions.