例外情况索引超出了数组的范围。“ [英] exception of " Index was outside the bounds of the array."

查看:108
本文介绍了例外情况索引超出了数组的范围。“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用系统的主要方法的代码第一行时获得异常

i am getting an exception while wrting that code fist line of main method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    delegate int MathFunction(int int1, int int2);
    class Program
    {

        static int Add(int int1, int int2)
        {
            return int1 + int2;
        }

        static int Subtract(int int1, int int2)
        {
            return int1 - int2;
        }

        static int Multiply(int int1, int int2)
        {
            return int1 * int2;
        }

        static int Divide(int int1, int int2)
        {
            return int1 / int2;
        }

        static void PrintResult(MathFunction mathFunction, int int1, int int2)
        {
            int result = mathFunction(int1, int2);
            Console.WriteLine(String.Format("Result is {0}", result));
        }
        static void Main(string[] args)
        {
             <big>int left = Convert.ToInt32(args[0]);</big>  //here i am getting exception
            char theOperator = args[1][0];
            int right = int.Parse(args[2]);
            MathFunction mathFunction;
            if (theOperator == '+')
                mathFunction = Add;
            else if (theOperator == '-')
                mathFunction = Subtract;
            else if (theOperator == '*')
                mathFunction = Multiply;
            else
                mathFunction = Divide;
            PrintResult(mathFunction, left, right);
        }
   
    }
}

推荐答案

谁告诉你 args [0] 存在吗?您的异常表示您将零参数传递给应用程序。

您是否知道 args 的目的?这是传递的参数数组。我希望你知道如何启动应用程序,如用户... :-)

-SA
Who told you that args[0] exists? Your exception indicates that you passed zero parameters to the application.
Do you know the purpose of args at all? This is the array of parameters passed. I hope you know how to start applications, as user… :-)
—SA


谢尔盖是对的。您的程序在启动时需要一些参数数组。要在visual studio中将参数传递给您的程序,请执行以下操作:

右键单击项目>属性>调试标签>启动选项>输入参数
Sergey is right. Your program expects some array of arguments when it starts. To pass arguments to your program in visual studio, do the following:
Right click on project > Properties > Debug tag > Start Options > enter arguments


你没有提供任何参数(args数组中没有值),这就是为什么它显示索引超出范围的异常。



您可以通过以下方式添加参数:

1)右键单击您的控制台应用程序项目。选择属性。

2)然后转到Debug选项卡。

3)在commmand行参数中添加字符串/值。换行符将结束你的论点。

4)保存属性。



提供的值将是你的参数数组(args [])哪个主要功能需要。
You have not supplied any arguments(no values in args array) thats why it shows index out of bound exception.

You can add arguments in below way:
1) Right click on your console application project. Select properties.
2) Then move to Debug tab.
3) Add string(s)/value(s) in commmand line arguments. A line break will end you argument.
4) Save properties.

Supplied values will be your argument array(args[]) which main function takes.


这篇关于例外情况索引超出了数组的范围。“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆