可能是一个非常简单的一个-我开始用c#和需要添加值到一个数组,例如:

int[] terms;

for(int runs = 0; runs < 400; runs++)
{
    terms[] = runs;
}

对于那些使用过PHP的人,下面是我试图在c#中做的事情:

$arr = array();
for ($i = 0; $i < 10; $i++) {
    $arr[] = $i;
}

当前回答

这里提供了关于如何使用数组来做到这一点的答案。

然而,c#有一个非常方便的东西叫做System。集合

集合是使用数组的理想替代品,尽管其中许多在内部使用数组。

例如,c#有一个名为List的集合,其功能与PHP数组非常相似。

using System.Collections.Generic;

// Create a List, and it can only contain integers.
List<int> list = new List<int>();

for (int i = 0; i < 400; i++)
{
   list.Add(i);
}

其他回答

如果你真的需要一个数组,下面可能是最简单的:

using System.Collections.Generic;

// Create a List, and it can only contain integers.
List<int> list = new List<int>();

for (int i = 0; i < 400; i++)
{
   list.Add(i);
}

int [] terms = list.ToArray();

这里提供了关于如何使用数组来做到这一点的答案。

然而,c#有一个非常方便的东西叫做System。集合

集合是使用数组的理想替代品,尽管其中许多在内部使用数组。

例如,c#有一个名为List的集合,其功能与PHP数组非常相似。

using System.Collections.Generic;

// Create a List, and it can only contain integers.
List<int> list = new List<int>();

for (int i = 0; i < 400; i++)
{
   list.Add(i);
}

只是不同的方法:

int runs = 0; 
bool batting = true; 
string scorecard;

while (batting = runs < 400)
    scorecard += "!" + runs++;

return scorecard.Split("!");

我将为另一个变体添加这个。我更喜欢这种类型的函数编码行。

Enumerable.Range(0, 400).Select(x => x).ToArray();

这对我来说似乎不那么麻烦:

var usageList = usageArray.ToList();
usageList.Add("newstuff");
usageArray = usageList.ToArray();