Can’t use an “inline” array in C#?

You have to create the array first, using new[].

string letter = (new[] {"a","b","c"}).AnyOne();

As @hvd mentioned you can do this without parantheses (..), I added the parantheses because I think it’s more readable.

string letter = new[] {"a","b","c"}.AnyOne();

And you can specify the data type new string[] as on other answers has been mentioned.


You can’t just do {"a","b","c"}, because you can think of it as a way to populate the array, not to create it.

Another reason will be that the compiler will be confused, won’t know what to create, for example, a string[]{ .. } or a List<string>{ .. }.

Using just new[] compiler can know by data type (".."), between {..}, what you want (string). The essential part is [], that means you want an array.

You can’t even create an empty array with new[].

string[] array = new []{ }; // Error: No best type found for implicity-typed array

Leave a Comment