我有一個應用程序,獲取3個主要參數:sum,substract,print。如果給出了這些參數之一,則應用程序會調用方法:求和的總和,減去的減去量,打印的打印量。在c#中使用參數/參數的正確方法?
用的參數調用所有的例子:
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.
我在做什麼錯?使用參數的最佳方式是什麼?
逐步通過您的代碼。 – tnw
代碼看起來不錯,您在調試時從哪裏提供參數(參數)? –
看看Giacomo Stelluti Scala的命令行解析器http://commandline.codeplex.com/documentation。 – Xaruth