我在以下 C# 程序中遇到问题。编写代码时没有错误,但是当我调试它时会抛出一个异常,指出索引超出了数组绑定(bind)的范围。
这里有什么问题?
using System;
class secmain
{
public static void squarearg(int i)
{
int m=i*i;
Console.WriteLine("The Square of the argument is {0}",m);
}
static void Main(String[] param)
{
Console.WriteLine("this program will convert your string argument to int and display the square of the numbe");
int k = Int32.Parse(param[0]);
squarearg(k);
}
}
最佳答案
很好地调试它(通常) 不会得到 command line parameter在 param
中,这就是您获得异常的原因。
你需要从命令行运行它的可执行文件并从那里传递参数,比如在你的dos提示符下,(cmd)
C:\yourProjectPath\bin\debug> yourExecutable.exe 2
(上面 2 是命令行参数,您将在 param[0]
中以字符串形式获取)
或者您可以使用 Console.ReadLine
从控制台获取值,然后处理它而不是命令行参数。
如果你想在调试过程中传递参数,那么检查这个问题:How do I start a program with arguments when debugging?
转到项目属性并在命令行参数文本框中的调试选项卡下指定命令行参数,如下图所示。
以上将为参数 2
提供结果 4
关于C#方 block 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14935418/