0

I would like to add a string array to a list of string arrays I have predefined. Here is a minimal working example:

using System;
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        List<string[]> listOfStringArrays = new List<string[]>();
        listOfStringArrays.Add({"hello", "world"});
    }
}

Unfortunately, the aboce example throws errors like the following:

C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\Program.cs(9,32): error CS1
026: ) expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]
C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\Program.cs(9,32): error CS1
002: ; expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]
002: ; expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]
C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\Program.cs(9,40): error CS1
513: } expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]
C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\Program.cs(9,49): error CS1
002: ; expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]
C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\Program.cs(9,50): error CS1
513: } expected [C:\Users\bm\desktop\temp\csharp_codility\add-string-array-to-list\console-cu
stom.csproj]

The build failed. Fix the build errors and run again.

What is wrong with my code? How can I fix it?

user32882
  • 5,094
  • 5
  • 43
  • 82
  • 2
    Typo: you're missing `new []` before the curly brace.`listOfStringArrays.Add(new []{"hello", "world"});` – gunr2171 Jun 09 '22 at 14:17
  • You need to use the `new` keyword in this context: https://stackoverflow.com/questions/30509177/cant-use-an-inline-array-in-c – ethandjay Jun 09 '22 at 14:19
  • In that case I wonder why the docs state that its OK to declare an array explicitly like this https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/#example – user32882 Jun 09 '22 at 14:19
  • I'm going to edit your post with your actual question then – gunr2171 Jun 09 '22 at 14:20
  • 2
    @user32882: It *is* okay to declare an array explicitly like that. But you're *not* declaring a variable, you're just passing an argument. The syntax you're trying to use is *only* valid within a variable declaration. – Jon Skeet Jun 09 '22 at 14:26
  • 1
    It's unfortunate that you thought my edit was "annoying" @user32882. I believe it made the actual problem much more clear. Can you explain what was incorrect about it? – gunr2171 Jun 09 '22 at 14:30
  • @gunr2171 The edit basically changed the nature of what I was asking. While the underlying *why* is interesting, I don't care so much about it in this question. I just needed the correct implementation for my code to build and run. – user32882 Jun 09 '22 at 14:47
  • So then my very first comment, pointing out the typo, was correct. – gunr2171 Jun 09 '22 at 14:52
  • @gunr2171 Sure... I never said it wasn't. If you'd like to post that as the final answer I'd be very happy to accept it. – user32882 Jun 09 '22 at 14:56
  • The duplicate link already provides an answer to your question, and questions that are resolved due to a typo are not useful to future readers. – gunr2171 Jun 09 '22 at 14:59
  • Well then vote to close I guess? – user32882 Jun 09 '22 at 15:02

0 Answers0