Question
What array initialization syntaxes are available in C#? Please explain the different valid ways to declare and initialize arrays, including examples of common patterns and when each form is used.
Short Answer
By the end of this page, you will understand the main ways to declare and initialize arrays in C#, how array size and values are assigned, when shorthand syntax works, and which forms are most commonly used in real code.
Concept
In C#, an array is a fixed-size collection of elements of the same type. Array initialization syntax is the set of rules C# gives you for:
- declaring an array variable
- creating the array object
- optionally setting its initial values
This matters because arrays are one of the most common data structures in C#. You use them when you know the number of items ahead of time and want fast indexed access.
A C# array always has:
- an element type such as
int,string, orbool - a rank such as one-dimensional (
[]) or multidimensional ([,]) - a fixed length once created
Here is the basic idea:
int[] numbers;
This declares a variable named numbers that can later refer to an array of integers.
To actually create the array:
int[] numbers = new int[3];
This creates space for 3 integers. Because no values were provided, C# fills the array with default values:
0forintfalseforboolnullfor reference types likestring
You can also create and fill the array at the same time:
int[] numbers = new int[] { 1, 2, 3 };
Or use the shorter form when the type is already known:
int[] numbers = { 1, 2, 3 };
These syntaxes exist to make code clearer in different situations. Some are more explicit, while others are more concise.
Mental Model
Think of an array like a row of numbered boxes.
- The type tells you what is allowed in each box.
- The length tells you how many boxes exist.
- The initializer tells you what items go into the boxes at the start.
Examples:
new int[3]means: “Create 3 boxes for integers.”new int[] { 1, 2, 3 }means: “Create enough integer boxes for these 3 values and place them in order.”int[] numbers = { 1, 2, 3 }means the same thing, just with less typing.
So array initialization is really about choosing between:
- I know the size
- I know the values
- I want both declaration and setup in one place
Syntax and Examples
Core syntax
1. Declare only
int[] numbers;
This declares a variable, but no array exists yet.
2. Create an array with a fixed size
int[] numbers = new int[5];
This creates an array of length 5 filled with default values:
// numbers = { 0, 0, 0, 0, 0 }
3. Create and initialize with values
int[] numbers = new int[] { 10, 20, 30 };
C# counts the items and creates an array of length 3.
4. Short array initializer
int[] numbers = { 10, 20, 30 };
This is a shorter version of the previous syntax.
5. Separate declaration and later assignment
Step by Step Execution
Consider this example:
int[] scores = { 90, 80, 70 };
Console.WriteLine(scores[1]);
Step by step
int[] scoresdeclares a variable that will store a reference to an integer array.{ 90, 80, 70 }tells C# the starting values.- C# creates an
int[]array of length 3. - The values are stored by index:
scores[0] = 90scores[1] = 80scores[2] = 70
Console.WriteLine(scores[1])accesses the second element.- The output is:
80
Another example with default values
string[] words = new string[2];
Console.WriteLine(words[0] == null);
Step by step:
Real World Use Cases
Arrays are useful when the number of items is known and fixed.
Common use cases
-
Storing fixed configuration values
string[] supportedFileTypes = { ".jpg", ".png", ".gif" }; -
Representing game board data
char[,] board = { { 'X', 'O', 'X' }, { 'O', 'X', 'O' }, { 'X', ' ', 'O' } }; -
Handling command-line arguments
static void Main(string[] args) { Console.WriteLine(args.Length); } -
Storing sensor readings or scores
double[] readings = { 21.5, 21.7, , };
Real Codebase Usage
In real C# projects, developers often use array initialization in a few predictable ways.
1. Static known values
private static readonly string[] AllowedRoles = { "Admin", "Editor", "Viewer" };
This is common for constants, allowed values, and small lookup data.
2. Method arguments using arrays
string.Join(", ", new string[] { "A", "B", "C" });
Sometimes the explicit new string[] form is useful when passing an array directly.
3. Validation and membership checks
string[] validStatuses = { "Open", "Closed", "Pending" };
Then the array may be checked using loops or LINQ.
4. Jagged arrays for uneven grouped data
int[][] monthlySales =
{
new [] { , , },
[] { , },
[] { , , , }
};
Common Mistakes
1. Confusing declaration with creation
Broken code:
int[] numbers;
Console.WriteLine(numbers.Length);
Problem:
numbersis declared but not assigned an array yet.- Using it causes a compile-time error because the local variable is not initialized.
Fix:
int[] numbers = new int[3];
Console.WriteLine(numbers.Length);
2. Using the short initializer outside a declaration
Broken code:
int[] numbers;
numbers = { 1, 2, 3 };
Problem:
- The shorthand
{ 1, 2, 3 }only works in a declaration.
Fix:
int[] numbers;
numbers = new int[] { 1, 2, 3 };
3. Mixing incompatible element types
Comparisons
| Concept | Example | Best when | Notes |
|---|---|---|---|
| Declare only | int[] a; | You will assign later | No array exists yet |
| Fixed size, default values | int[] a = new int[3]; | You know the length but not the values yet | Elements get default values |
| Explicit initialization | int[] a = new int[] { 1, 2, 3 }; | You want clarity or need assignment after declaration | Works in more places |
| Short initialization | int[] a = { 1, 2, 3 }; | You are declaring and initializing in one line | Most concise |
Arrays vs List
Cheat Sheet
// Declare only
int[] a;
// Create fixed size with default values
int[] a = new int[3];
// Create and initialize explicitly
int[] a = new int[] { 1, 2, 3 };
// Create and initialize with shorthand
int[] a = { 1, 2, 3 };
// Assign later
int[] a;
a = new int[] { 1, 2, 3 };
// Multidimensional array
int[,] grid = new int[,]
{
{ 1, 2 },
{ 3, 4 }
};
// Jagged array
int[][] jagged = new int[][]
{
new int[] { 1, 2 },
new int[] { 3, 4, 5 }
};
FAQ
Can I declare an array without initializing it in C#?
Yes. You can write int[] numbers;, but you cannot use it until it refers to an actual array.
What is the shortest way to initialize an array in C#?
The shortest common form is:
int[] numbers = { 1, 2, 3 };
Why does numbers = { 1, 2, 3 }; not compile?
Because the short initializer syntax only works at the point of declaration. For later assignment, use:
numbers = new int[] { 1, 2, 3 };
What values does a new array contain by default?
Default values based on the element type, such as 0, false, or null.
What is the difference between int[,] and int[][]?
int[,] is a rectangular multidimensional array. int[][] is a jagged array, meaning an array whose elements are arrays.
Mini Project
Description
Build a small C# console program that demonstrates the main array initialization syntaxes in one place. This helps reinforce the differences between declaration, fixed-size creation, shorthand initialization, multidimensional arrays, and jagged arrays.
Goal
Create a program that initializes several arrays in different valid ways and prints their contents and lengths.
Requirements
- Declare an array and assign it later.
- Create one array using a fixed size and default values.
- Create one array using the short initializer syntax.
- Create and print one multidimensional array.
- Create and print one jagged array.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Type Checking Explained: typeof vs GetType() vs is
Learn when to use typeof, GetType(), and is in C#. Understand exact type checks, inheritance, and safe type testing clearly.