Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use an "inline" array in C#?

Tags:

c#

Imagine you have this somewhere

public static T AnyOne<T>(this T[] ra) where T:class     {     int k = ra.Length;     int r = Random.Range(0,k);     return ra[r];     } 

or even just this

public static string OneOf(this string[] strings)     {     return "a";     } 

Then, of course you can do this...

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

... which is great. BUT. It would appear you can NOT do this:

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

or indeed perhaps this

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

or anything else I tried.

In fact (1) why can one not do that? and (2) am I missing something, how would you do that if there is a way?

like image 949
Fattie Avatar asked May 28 '15 14:05

Fattie


People also ask

How do you initialize an array of objects in C#?

Initialize Arrays in C# with Known Number of Elements We can initialize an array with its type and size: var students = new string[2]; var students = new string[2]; Here, we initialize and specify the size of the string array.

How do you declare and initialize an array in one line?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.


2 Answers

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 
like image 78
adricadar Avatar answered Oct 13 '22 01:10

adricadar


(1) why can one not do that? {"a","b","c"}.AnyOne();

This line:

string[] st = {"a","b","c"}; 

is a short hand for an equivalent array creation expression (under ILSpy)

string[] st = new string[]  {"a","b","c"}; 

This string[] st = {"a","b","c"} can only be used at the time of declaration, you can't not use it elsewhere, you can't even do:

string[] st; st = {"a", "b", "c"}; //Error 

It is explained under Section 7.6.10.4 for Array Creation expression in C# language specifications.

So this "{"a", "b", "c"}" alone without usage in declaration means nothing. Hence you can't use it with your extension method, as your extension method operates on an array.

(2) am I missing something, how would you do that if there is a way?

Already mentioned in @adricadar's answer, you can do:

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

or

(new string[] {"a","b","c"}).AnyOne(); 
like image 45
Habib Avatar answered Oct 13 '22 00:10

Habib