创建C#INT []与值0,1,2,3 ...长度 [英] Create c# int[] with value as 0,1,2,3... length

查看:393
本文介绍了创建C#INT []与值0,1,2,3 ...长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个 INT [] 长度 X 和[0,1,2它的价值.... X]

I like to create an int[] with length X and value it with [0,1,2....X]

例如。

公众诠释[] CreateAA(INT X){}

INT [] AA = CreateAA(9)=> [0,1,2,3,4,5,6,7,8,9]

有没有简单的方法是什么?或有环和初始化值

is there any easy method? Or have to loop and init value

推荐答案

您可以利用的IEnumerable的功能。

You can avail the functionality of IEnumerable.

int[] arr = Enumerable.Range(0, X+1).ToArray();

这会为你创建一个IEnumerable的列表和 .ToArray()将满足您的int数组的需要。

This will create a IEnumerable List for you and .ToArray() will satisfy your int array need.

所以,针对你的情况X = 9,将产生 [0,1,2,3,4,5,6,7,8,9] (根据需要)

So for X=9 in your case it would generate the array for [0,1,2,3,4,5,6,7,8,9] (as you need)

这篇关于创建C#INT []与值0,1,2,3 ...长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆