我正在尝试使用c#接口概念编写小型程序用于circle&区域。 square.While给出具体条件if (args[0] == "S")
时出现错误IndexOutOfRangeException
:
if (args[0]=="S")
fig = new Square();
if (args[0]=="C")
fig = new Circle();
答案 0 :(得分:0)
如果args
为空,则会发生这种情况。你不能要求空数组的第一个元素,因为没有一个。你应该先检查长度:
if (args.Length == 0)
{
// Maybe exit? Is it valid not to specify any arguments?
}
// Either use an "else" here, or if you've quit in the "if" block
// then you don't need to because you know that there's at least
// one argument by now