Compiler Error CS0178

Switch View :
ScriptFree
Visual C# Reference: Errors and Warnings
Compiler Error CS0178

Invalid rank specifier: expected ',' or ']'

An array initialization was ill-formed. For example, when specifying the array dimensions, you can specify the following:

  • A number in brackets

  • Empty brackets

  • A comma enclosed in brackets

For more information, see Arrays (C# Programming Guide) and the C# specification (C# Language Specification) section on array initializers.

Example

The following sample generates CS0178.

// CS0178.cs
class MyClass
{
   public static void Main()
   {
      int a = new int[5][,][][5;   // CS0178
      int[,] b = new int[3,2];   // OK

      int[][] c = new int[10][];
      c[0] = new int[5][5];   // CS0178
      c[0] = new int[2];   // OK
      c[1] = new int[2]{1,2};   // OK
   }
}
Community Content

RolanDecoy
Errors with custom textures using XNA
I found that this error occurs when declaring an array of colors for custom Texture2D objects:


private Texture2D Generate_Texture(GraphicsDevice Device) { // Ok
Texture2D Result = new Texture2D(Device, 9, 9); // Ok
Color[] Colors = new Color[6]; // Ok
Color[] Data = new Color[] { Colors[0], Colors[5] , Colors[2] }; // CS0178 @ {
Result.SetData(Data); // Ok
return Result; // Ok
}

This is weird, since I tried using a basic datatype (int and byte) instead of Color and it works fine, which leads me to believe that the code and syntax are correct, but its the object type that mess things up. And it gets weirder when it does this during editing, not compiling nor runtime.