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

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.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...