【问题标题】:Check if Array Position Exists检查数组位置是否存在
【发布时间】:2013-04-11 10:14:50
【问题描述】:

我正在尝试检查数组的位置是否存在。

我正在尝试从列表中输出位置 1、2、3、4 和 5 字符串值。列表小于5的地方,需要显示一个'-'作为字符串值。

例如,应显示 3 个列表:Value, Value, Value, -, -

但是我无法弄清楚如何检查这个,并且我不断收到 index is out of range 错误。

我试过了:

if (String.IsNullOrEmpty(formGuideCount[3]))
{
    game4 = formGuideCount[3];
}
else
{
    game4 = "-";
}

谁能告诉我应该使用什么来检查该职位是否不存在?

谢谢

【问题讨论】:

    标签: c# arrays list


    【解决方案1】:

    您可以使用数组的Array.Length 来验证索引位置是否存在。

    if(formGuideCount.Length > 3)
    {
          game4 = formGuideCount[3];
    }
    

    【讨论】:

      【解决方案2】:

      您可以查看yourArray.Length

      如果你使用的是多维数组,你可以这样做:

      yourArray.GetLength(0)  //first dimension length
      yourArray.GetLength(1)  //second dimension length
      // and so on
      

      【讨论】:

        【解决方案3】:

        使用数组的Length 成员

        if (formGuideCount.Length > 3)
        {
            game4 = formGuideCount[3];
        }
        else
        {
            game4 = "-";
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-20
          • 1970-01-01
          • 2013-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多