2016-01-27 44 views
-1

我使用C#界面概念圆& square.While给予特定条件if (args[0] == "S")面积有错误IndexOutOfRangeException试图写小程序:错误:指数是阵列的C#中的边界之外

if (args[0]=="S") 
    fig = new Square(); 
if (args[0]=="C") 
    fig = new Circle(); 
+0

您不检查args数组是否具有非零长度。如果您将代码包含在文本中而不是链接到图像,那将会更好。 –

回答

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 
相关问题