0

I don't understand why this is outside the bounds of the array? This array is supposed to accept these command line arguments but every time I compile I have been thrown this exception. I am new to programming so this could be obvious.

Code:

public static void Main(string[] args)
{
    string[] CommandLine = new string [4];

    string configFile = args[0];
    string file1 = args[1];
    string file2 = args[2];

    Console.WriteLine("configfile is " + configFile);
    Console.WriteLine("file1 is " + file1);
    Console.WriteLine("file2 is " + file2);
    Console.Read();
}

Error: Index was outside the bounds of the array.

Evaldas Buinauskas
  • 12,532
  • 10
  • 45
  • 88
Rory
  • 29
  • 2

1 Answers1

3

string[] args is a special parameter here, this is an array of command line arguments passed to the application during start-up. If there are no arguments passed in - you will get exactly this exception. Or as in your case, no argument within args with that index.

You can inspect your args during debugging and see what are the actual values of it.

Evaldas Buinauskas
  • 12,532
  • 10
  • 45
  • 88