C# Error CS0022 – Wrong number of indices inside [], expected ‘number’

C# Compiler Error Message

Wrong number of indices inside [], expected ‘number’

Reason for the Error

You will get the C# compiler error CS0022 when you specify the in-correct number of dimensions with-in the square brackets when assigning values to array in C#.

For example, the below code snippet will result with the error CS0022 Wrong number of indices inside []; expected 1

public class DeveloperPublish
{
    public static void Main()
    {
        int[] array = new int[10];
        array[0] = 17;    
        // Assigning to 2D- Arry results in the CS0022
        array[0, 1] = 2;   
    }
}
C# Error CS0022 - Wrong number of indices inside [], expected 'number'

Solution

Fix the error by providing the right dimensions for the array with-in square brackets.

Copy link
Powered by Social Snap