Search code examples
c#.netarrayslinqinitialization

c# Leaner way of initializing int array


Having the following code is there a leaner way of initializing the array from 1 to the number especified by a variable?

int nums=5;
int[] array= new int[nums];

for(int i=0;i<num;i++)
{
   array[i] = i;
}

Maybe with linq or some array.function?


Solution

  • int[] array = Enumerable.Range(0, nums).ToArray();