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

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...