How to Resolve Error "CS0847: An array initializer of length 'value' is expected" in C#
The Compiler Error CS0847 is a structural consistency error regarding Rectangular Arrays. The message reads: "An array initializer of length 'X' is expected".
In C#, a Multi-dimensional Array (defined with commas like int[,]) represents a strict grid (a matrix). Unlike a "Jagged Array" (array of arrays), a Rectangular Array must be uniform. Every row must have exactly the same number of columns; every block must have the same depth.
This error occurs when you provide an initializer list where the rows have inconsistent lengths (e.g., Row 1 has 3 items, but Row 2 has only 2 items).
Understanding Rectangular Consistency
When the compiler builds a T[,], it allocates a single contiguous block of memory. It calculates the memory offset using the formula Row * Width + Column. For this math to work, Width must be constant for every single row.
- Valid:
[2, 2]->{ {1, 2}, {3, 4} } - Invalid:
[2, ?]->{ {1, 2, 3}, {4, 5} }(CS0847)
Scenario 1: Inconsistent Row Lengths in 2D Arrays
This usually happens when manually typing data into an initializer and missing a comma or a value. The compiler infers the length of the first row as the standard, and expects all subsequent rows to match it.
Example of error
Here, the first row has 3 elements. The compiler now enforces a width of 3. The second row only has 2 elements.
public class MatrixLoader
{
public void LoadData()
{
// ⛔️ Error CS0847: An array initializer of length '3' is expected.
// Row 0: 3 items (1, 2, 3)
// Row 1: 2 items (4, 5) -> Mismatch!
var matrix = new[,]
{
{ 1, 2, 3 },
{ 4, 5 }
};
}
}
Solution: Make Rows Uniform
Fill in the missing data so that every row has the exact same count.
public class MatrixLoader
{
public void LoadData()
{
// ✅ Correct: Both rows have 3 items.
var matrix = new[,]
{
{ 1, 2, 3 },
{ 4, 5, 0 } // Added '0' padding
};
System.Console.WriteLine($"Dimensions: {matrix.GetLength(0)}x{matrix.GetLength(1)}");
}
}
Scenario 2: Intending to use Jagged Arrays
Sometimes, the data is supposed to be uneven (e.g., a list of teams where each team has a different number of players). If you use a Rectangular Array [,] for this, you will fight the compiler. You should be using a Jagged Array [][] (an array of arrays).
Example of error
Trying to force uneven data into a Rectangular grid.
public class TeamRoster
{
public void CreateTeams()
{
// ⛔️ Error CS0847: Cannot fit uneven lists into a [,] matrix.
string[,] teams = new[,]
{
{ "Alice", "Bob" },
{ "Charlie", "David", "Eve" }
};
}
}
Solution: Use Jagged Arrays ([][])
Change the syntax from [,] to [][]. This allows each "row" to be an independent array with its own length.
public class TeamRoster
{
public void CreateTeams()
{
// ✅ Correct: Using a Jagged Array (Array of Arrays).
// Note the syntax change: 'new[] { ... }' for inner arrays.
string[][] teams = new string[][]
{
new[] { "Alice", "Bob" },
new[] { "Charlie", "David", "Eve" }
};
System.Console.WriteLine($"Team 1 Size: {teams[0].Length}"); // 2
System.Console.WriteLine($"Team 2 Size: {teams[1].Length}"); // 3
}
}
Key Difference:
- Rectangular
[,]: High performance, single block of memory, strictly uniform. - Jagged
[][]: Flexible, multiple objects in memory, variable lengths allowed.
Conclusion
CS0847 enforces the shape of your grid.
- Check the Data: Look at your initialization list. Does one row have more items than the others?
- Pad the Data: If using
[,], add default values (0 or null) to short rows to make them match the longest row. - Switch Structure: If the data naturally varies in length, switch to a Jagged Array (
[][]) instead of a Rectangular Array.