【发布时间】:2014-02-24 15:14:09
【问题描述】:
我有一个应用程序,它获取 3 个主要参数:求和、减法、打印。如果给定了其中一个参数,应用程序将调用方法:sum 为 sum,substract 为 substract,print 为 print。
带参数调用所有例子:
myapp.exe sum 1 2
myapp.exe substract 3
myapp.exe print whatever
这是我的实际代码,它不起作用
static void Main(string[] args)
{
Program package = new Program();
if (args[0] == @"sum")
{
package.sum(args[1], args[2]);
}
else if (args[0] == @"substract")
{
package.substract(args[1]);
}
else if (args[0] == @"print")
{
package.print(args[1]);
}
else
Console.Write("Invalid Parameters");
}
但这是我调试时遇到的错误:
A first chance exception of type 'System.IndexOutOfRangeException' occurred in myapp.exe
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in myapp.exe
Additional information: Index was outside the bounds of the array.
我做错了什么?使用参数的最佳方式是什么?
【问题讨论】:
-
单步调试您的代码。
-
代码看起来OK,调试的时候在哪里提供参数(参数)?
-
看看 Giacomo Stelluti Scala 的 Command Line Parser commandline.codeplex.com/documentation。
标签: c# parameters arguments