0

The question is in title.

What does comma mean as a length of array in C#?

How it is called, how to find it on the Internet?

I searched for "how to initialize array in C#" in google but there is no information about this comma. If I remove comma VS shows an error: "array initializers can only be used in a variable". Even after assigning it to variable it still shows error.

EntityFramework generates the following code:

migrationBuilder.InsertData(
            table: "Test",
            columns: new[] { "Name" },
            values: new object[,]
            {
                { "Test" },
                { "Test1" }
        });
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • 2
    Does this answer your question? [What are the differences between a multidimensional array and an array of arrays in C#?](https://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – phuzi Mar 12 '20 at 12:11
  • 1
    Without initializer, you can do `var my2DArray = new object[3,2];` - the comma separates the dimensions – Hans Kesting Mar 12 '20 at 12:19
  • Thank you guys. It helped. I didn't event guess it. – Vitaliy Goncharov Mar 12 '20 at 13:37

1 Answers1

6

The coma does not represent a length, it represent a dimension. In your case, it can be called a 2d array. It allows you to do this:

values: new object[,]
{
    { "Testa", "Testb", "Testc" },
    { "Test1a", "Test1b", "Test1c" }
}
the_lotus
  • 12,668
  • 3
  • 36
  • 53