Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Array Initialization: Syntax and Common Patterns

C# arrays store a fixed number of elements. Learn common initialization forms, default values, rectangular and jagged arrays, C# 12 collection expressions, and how to choose between arrays and List<T>.

~3 min readC#
#Array#C# Syntax#.NET#Initialization
Diagram showing different C# array initialization syntax forms with a visual representation of an array structure.

Arrays in C# store a fixed number of elements of the same type. The initialization syntax is simple, but the right form depends on whether you know the values up front, whether the array is rectangular or jagged, and which C# version the codebase uses.

The common C# array initialization forms are:

csharp
int[] numbers = new int[5]; // length 5, all elements 0 int[] explicitValues = new int[] { 1, 2, 3, 4, 5 }; int[] shorthand = { 1, 2, 3, 4, 5 }; var inferred = new[] { 1, 2, 3, 4, 5 };

The first form allocates an array of length 5 and sets each element to the default value for the element type. For int that is 0; for reference types such as string, it is null. The second and third forms are equivalent; the shorthand works only when the declaration includes the initializer. The var form lets the compiler infer the element type from the values.

Default Values and Allocation

When you specify only the length, every element is initialized to the default value for the element type:

csharp
string[] names = new string[3]; // all null bool[] flags = new bool[2]; // all false

For value types, that value is the result of default(T); for reference types, it is null. The array variable refers to a contiguous block on the managed heap, so the runtime must allocate enough memory for the element count. A very large allocation can fail with OutOfMemoryException; the practical limit depends on available memory and the runtime. Arrays also have a fixed length, which makes them appropriate when the size is known in advance and does not need to change.

Rectangular and Jagged Arrays

C# has two multi-dimensional array forms.

A rectangular array has a fixed number of rows and columns, and every row has the same length:

csharp
int[,] matrix = new int[3, 4]; // 3 rows, 4 columns, all zeros int[,] explicitMatrix = new int[,] { { 1, 2 }, { 3, 4 } };

A jagged array is an array of arrays. Each inner array can have its own length, so you initialize each one separately:

csharp
int[][] jagged = new int[3][]; jagged[0] = new int[] { 1, 2 }; jagged[1] = new int[] { 3, 4, 5 }; jagged[2] = new int[] { 6 };

You can also initialize a jagged array inline:

csharp
int[][] jagged = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4, 5 } };

Jagged arrays can use less memory than rectangular arrays when row lengths vary, because you allocate only the space you need. They do add one level of indirection when accessing elements, which can matter in tight loops.

Collection Expressions in C# 12

C# 12 added collection expressions, which let you initialize arrays with square brackets:

csharp
int[] numbers = [1, 2, 3, 4, 5];

This produces the same runtime behavior as new int[] { 1, 2, 3, 4, 5 }. Collection expressions also work for jagged arrays:

csharp
int[][] jagged = [[1, 2], [3, 4, 5]];

Because this is a newer language feature, an older C# compiler will not accept the syntax. Use the traditional initializer forms in codebases that have not upgraded.

Common Pitfalls

Array covariance can cause runtime exceptions. Because C# allows an array of a derived type to be assigned to an array of its base type, a string[] can be treated as an object[]. Writing an incompatible value into it fails at runtime:

csharp
object[] objects = new string[3]; objects[0] = 1; // throws ArrayTypeMismatchException

Use List<T> or another generic collection when you want compile-time type safety.

Empty arrays are valid and non-null. If you often need an empty array, Array.Empty<int>() avoids allocating a new instance each time:

csharp
int[] empty = Array.Empty<int>();

Performance and Memory Considerations

Creating an array with a length-only initializer requires the runtime to allocate the block and set each element to the default value, so the cost grows linearly with length. Creating an array with explicit values stores each value into the allocated block. If you need to add items incrementally, List<T> is usually more convenient because it amortizes resizing. If the size is fixed ahead of time and you want low-overhead indexed access, an array is the more direct choice. Arrays are also the underlying storage for many collection types and are commonly used with Span<T> and interop code.

Choosing Between Array and List for Initialization

Use an array when the number of elements is fixed and known in advance. Use List<T> when the collection must grow or shrink while data is loaded. For example, reading lines from a file works naturally with a List<string> because the line count is not known ahead of time:

csharp
List<string> lines = new List<string>(); while (reader.ReadLine() is string line) { lines.Add(line); }

With an array, you would need to know the length up front or copy from a temporary list, and the copy adds an extra pass over the data. In application code, choose the collection type that matches the usage pattern: fixed-size, homogeneous data favors arrays; varying size or frequent insert/remove operations favors List<T>.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.