我将值分配给变量,如下所示:
private string[] numbers = new string[] {"1" , "2" , "3" , "4" , "5"};
然后我拿出我的数字变量,我按照以下过滤每个
protected void display()
{
bonusdiv.Visible = true;
panelbonus.InnerHtml = "";
foreach (string option in numbers)
{
string optiontext = option;
if (option.Length == 1)
{
optiontext = "" + optiontext;
}
if (panelBonusselections.Contains(option))
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"optionselected\"/>";
}
else
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"option\"/>";
}
}
}
现在我提出的问题是,我想将上面提到的值分配到单个实例中,例如
numbers=[5]; //but this must still use the same logic as above to store the values from 1 up to 5.Then how do i cater for this change in my foreach loop?
它会像波纹管一样吗?
int numbers = [5]
for (i=1,i>5, i++) {
foreach (string option in numbers)
{
string optiontext = option;
if (option.Length == 1)
{
optiontext = "" + optiontext;
}
if (panelBonusselections.Contains(option))
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"optionselected\"/>";
}
else
{
panelbonus.InnerHtml += "<input name=\"_bonus1_" + option + "\" id=\"/_bonus1_" + option + "\" type=\"submit\" value=\"" + optiontext + "\" class=\"option\"/>";
}
答案 0 :(得分:2)
您可以使用LINQ&#39; Enumerable.Range()
来创建序列,并使用Select()
将其投影到字符串:
string[] numbers = Enumerable.Range(1, 5).Select(i => i.ToString()).ToArray();
或作为一种方法:
string[] GetNumbersStringArray(int length)
{
return Enumerable.Range(1, length).Select(i => i.ToString()).ToArray();
}