C# Error CS0178 – Invalid rank specifier: expected ‘,’ or ‘]’

C# Compiler Error

CS0178 – Invalid rank specifier: expected ‘,’ or ‘]’

Reason for the Error

You will receive this error in your C# code when the Array initialization is ill-formed with-in your program.

For example, lets try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] inputArray = new int[10][1];
        
        }
    }
  
}

The above code snippet will result with the error code CS0178 because the array inputArray declaration and initialization seems to be ill-formed.

Error CS0178 Invalid rank specifier: expected ‘,’ or ‘]’ ConsoleApp3 C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 8 Active

Solution

To fix the error, ensure that the array initialization is correct and uses the right format when you initialize the 1-D, 2D or Multi-dimensional arrays in C#.

The error code CS0178 in the above program can be fixed by replacing the array initialization as shown below.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] inputArray = new int[10, 1];      
        }
    }
  
}

Leave A Reply

Your email address will not be published. Required fields are marked *

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...