Is there a simple way to create an initialized array?

al-*_*bex 3 c#

I want to create an int[n] array of unique numbers:

int[] result = new int[24];

for(int i = 0; i<24; i++)
    result[i] = 1;

return result;
Run Code Online (Sandbox Code Playgroud)

Is there a shorter way to accomplish this. May be something like this:

return (from i in new int[24] 
        select 1).ToArray();
Run Code Online (Sandbox Code Playgroud)

But not as ugly as this.

Jon*_*eet 11

我不确定以什么方式使它们全部为"1"使它们独一无二,但这将与您的代码相同:

return Enumerable.Repeat(1, 24).ToArray();
Run Code Online (Sandbox Code Playgroud)


Mar*_*ris 5

您发布的代码似乎与问题的标题不符,但这与您的代码段相同:

Enumerable.Repeat(1, 24).ToArray()
Run Code Online (Sandbox Code Playgroud)