索引超出了数组界限.如何解决这个错误

如题所述

"索引超出了数组界限"并不是说索引有多长,
而是说这个索引在数组的界限当中找不到,
在楼主的代码中,
无法保证String[] args 一定有值(即可能不存在args[0]),

如果楼主是想在string[] args有值的情况下才输出第一个参数的话,
可以改成
class Program
{
static void Main(string[] args)
{
string strName; //声明一个string类型的值变量
if (args.Count() > 0)
{
strName = args[0];//把第一个参数赋给变量strName
Console.WriteLine("This is the first argument: {0}!", strName); //格式化输出第一个参数
}
}
}

如果楼主想不管有没有值都输出信息,
可以改成:
static void Main(string[] args)
{
string strName = "args is null"; //声明一个string类型的值变量(当数组string[] args 没值时,输出args is null)
if (args.Count() > 0)
{
strName = args[0];//把第一个参数赋给变量strName

}
Console.WriteLine("This is the first argument: {0}!", strName); //格式化输出第一个参数
}
温馨提示:答案为网友推荐,仅供参考
相似回答