开发者

More elegant way to create an array, where each element is the index

开发者 https://www.devze.com 2022-12-19 11:23 出处:网络
Is there more elegant way(LINQ 开发者_JAVA技巧or other) to initialize array like this? int[] result = new int[PageCount];

Is there more elegant way(LINQ 开发者_JAVA技巧or other) to initialize array like this?

 int[] result = new int[PageCount];
 for (int i = 0; i < PageCount; i++)
 {
      result[i] = i;
 }
 return result;


I'd use:

int[] result = Enumerable.Range(0, PageCount).ToArray();

It's not the speediest solution possible, but it's unlikely to be a bottleneck in most apps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号