I am trying to write a program in which, I want to initialize 4 starting values of an array, while the other 16 will be entered manually from the console.
For this I have written the following code:
int[] intArray = new int[20] { 20, 22, 23, 0 };
But I am getting the following exception: "An array initializer of '20' is expected"
I know that it can be done by this syntax :
int[] intArray = new int[20];
intArray[0] = 20;
intArray[1] = 22;
intArray[2] = 23;
intArray[3] = 0;
I would like to know if this can be achieved in one line.
new[] { 20, 22, 23, 0 }.Concat(Enumerable.Repeat(0, 16)).ToArray()would do it, for example...int[] intArray = new int[20]; intArray[0] = 20; intArray[1] = 22; intArray[2] = 23; intArray[3] = 0;as one line, but it is the same number of statements...