简体   繁体   中英

Index was out of the bounds of the array

This is my program with error:

index was out of the bounds of array

code:

using System;
using System.Collections.Generic;

using System.Text;

namespace command
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("First Name is " + args[0]);
            Console.WriteLine("Last Name is " + args[1]);
            Console.ReadLine();
        }
    }
}

The problem is that you're not passing any parameters to your program. You can do this by running your program from the command line, or if you are running this through Visual Studio, you can set the arguments to pass by going to the project properties, selecting the Debug tab, and entering them into the Start options section.

Visual Studio 项目属性

You have to check how many command line parameters are actually provided :

using System;
using System.Collections.Generic;

using System.Text;

namespace command
{
    class Program
    {
       static void Main(string[] args) { 
         if (args.Length > 0)
           Console.WriteLine("First Name is " + args[0]);

         if (args.Length > 1)
           Console.WriteLine("Last Name is " + args[1]);

         Console.ReadLine();
       }
    }
}

eg

  # No parameters
  c:\MyProgram.exe 

  # One parameter 
  c:\MyProgram.exe FirstNameOnly 

  # Two parameters 
  c:\MyProgram.exe FirstName LastName

This relies on the idea that you are passing a minimum of two arguments, but you are not verifying this first. If you don't want output unless the user has met a minimum of two arguments, then using a try{} block, and catch an out of bounds array with a response to the user to use a minimum of two arguments.

   static void Main(string[] args) { 
       try{
       Console.WriteLine("First Name is " + args[0]);

        Console.WriteLine("Last Name is " + args[1]);

        Console.ReadLine();
   }catch (OutOfBoundsException exception){
     MessageBox.Show("Insufficient input parameters");
   }

You could also use an if(args.Length == 2) to determine if this is enough without using a try{}.

You always must check the size of the args array. SO...

static void Main(string[] args)
{
    if ( args.Count() >= 2 )
    {
        Console.WriteLine("First Name is " + args[0]);
        Console.WriteLine("Last Name is " + args[1]);
        Console.ReadLine();
    }
}

Try this:

static void Main(string[] args) 
{
    if(args.Length > 0)
    {
        Console.WriteLine("First Name is " + args[0]);
        Console.WriteLine("Last Name is " + args[1]);
    }
    else
        Console.WriteLine("No Command Line Arguments were passed");

    Console.ReadLine();
}

and pass the command line arguments as explained by David_001 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM