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?
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.
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.
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
(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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With