How to Resolve Error "CS0022: Wrong number of indices inside []; expected 'number'" in C#
The Compiler Error CS0022 is a dimensionality mismatch error. The message reads: "Wrong number of indices inside []; expected 'number'".
This error occurs when you attempt to access an element within an array (or a collection with an indexer) using the wrong number of coordinates. For example, trying to access a 2D grid using only one number, or trying to access a standard list using two numbers.
This guide explains the difference between standard arrays, multi-dimensional matrices, and jagged arrays to help you fix your indexing syntax.
Understanding Array Dimensions
In C#, square brackets [] are used to access data. The number of values you put inside the brackets depends on how the data structure was defined:
- 1D Array (
int[]): Requires 1 index. Example:arr[0] - 2D Array / Matrix (
int[,]): Requires 2 indices. Example:arr[0, 1] - Jagged Array (
int[][]): Requires chained indices. Example:arr[0][1]
CS0022 happens when the definition and the usage do not match.
Scenario 1: The Matrix (Multi-dimensional Arrays)
A common mistake is treating a multi-dimensional array (defined with a comma [,]) as if it were a single-dimensional list.
Example of Mistake
You define a grid (e.g., a chessboard or spreadsheet), but try to access a "row" using a single index. Unlike C++, accessing just the row of a C# [,] array is not allowed via indexing; you must specify both coordinates to get a value.
public void ProcessGrid()
{
// A 3x3 Matrix
int[,] matrix = new int[3, 3] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// ⛔️ Error CS0022: Wrong number of indices inside []; expected 2
// You cannot access a whole row like this in a Rectangular Array.
var val = matrix[0];
}
Solution
You must provide both the Row and the Column index.
public void ProcessGrid()
{
int[,] matrix = new int[3, 3];
// ✅ Correct: Access row 0, column 1
int val = matrix[0, 1];
}
Scenario 2: The Jagged Array Confusion
Jagged arrays are "arrays of arrays." They are defined with side-by-side brackets [][] rather than a comma [,]. A common error is trying to access them using the comma syntax specific to matrices.
Example of Mistake
Trying to use [x, y] syntax on a jagged array.
public void ProcessJagged()
{
// A Jagged Array (An array containing 3 arrays)
int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
// ⛔️ Error CS0022: Wrong number of indices inside []; expected 1
// The variable 'jagged' is technically a 1D array (holding other arrays).
// It expects 1 index to get the inner array.
// '[0, 1]' passes 2 indices, which is wrong for the outer array.
int val = jagged[0, 1];
}
Solution
Use chained brackets. The first [0] selects the inner array, and the second [1] selects the element within that inner array.
public void ProcessJagged()
{
int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
// ✅ Correct: "Get array at 0", then "Get item at 1 from that array"
int val = jagged[0][1];
}
Memory Aid:
- If the declaration has a comma
[,], the access needs a comma[x, y]. - If the declaration has separate brackets
[][], the access needs separate brackets[x][y].
Scenario 3: Standard Arrays and Lists
Sometimes, this error is a simple typo where a user accidentally adds an extra comma inside the brackets of a standard 1D array or List.
Example of Mistake
using System.Collections.Generic;
public void ProcessList()
{
List<string> names = new List<string> { "Alice", "Bob" };
// ⛔️ Error CS0022: Wrong number of indices inside []; expected 1
// Lists are 1-dimensional.
string s = names[0, 1];
}
Solution
Remove the extra arguments.
public void ProcessList()
{
List<string> names = new List<string> { "Alice", "Bob" };
// ✅ Correct
string s = names[0];
}
Conclusion
CS0022 is the compiler telling you that your coordinates don't match the map.
- Check the declaration: Look at how the array was created (
int[],int[,], orint[][]). - Match the syntax:
Type[]-> Usevar[x]Type[,]-> Usevar[x, y]Type[][]-> Usevar[x][y]