C#方 block 程序

标签 c# .net arguments

我在以下 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 parameterparam 中,这就是您获得异常的原因。

你需要从命令行运行它的可执行文件并从那里传递参数,比如在你的dos提示符下,(cmd)

C:\yourProjectPath\bin\debug> yourExecutable.exe 2

(上面 2 是命令行参数,您将在 param[0] 中以字符串形式获取)

或者您可以使用 Console.ReadLine 从控制台获取值,然后处理它而不是命令行参数。

如果你想在调试过程中传递参数,那么检查这个问题:How do I start a program with arguments when debugging?

转到项目属性并在命令行参数文本框中的调试选项卡下指定命令行参数,如下图所示。

enter image description here

以上将为参数 2 提供结果 4

关于C#方 block 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14935418/

相关文章:

c# - 捕获 C# 2.0 中作为类型参数给出的异常类型

undefined - sass @mixin 可以接受未定义数量的参数吗?

python - 如何将字典作为位置参数而不是关键字参数传递

c# - 雅阁机器学习 knn.decide indexOutOfRangeException

c# - 格式化 xml 输出

c# - 如何从 ffmpeg 异步流式输出

asp.net - PC 区域和语言设置在 .Net 中被忽略

namespaces - tcl 切换命名空间和 proc 参数

c# - ASP.NET MVC3 升级 - "Attempt by method [whatever] to access method System.Web.Mvc.Controller.View(...) failed"

c# - dataGridView 绑定(bind)到 List - 将 bool 转换为 Image