Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
// Namespace Declaration
using System;

// Program start class
class NamedWelcome
{
    // Main begins program execution.
    static void Main(string[] args)
    {
        // Write to console
        Console.WriteLine("Hello, {0}!", args[0]);
        Console.WriteLine("Welcome to the C# Station Tutorial!"); 
    }
}


Every time i fire up this code in visual studio, it shoots an error!

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in ConsoleApplication1.exe

Additional information: Index was outside the bounds of the array.


I couldnt understand why does this happens?
I am a newbee to c# so please help!!
Posted
Comments
[no name] 4-Aug-13 13:54pm    
Because args is empty
AbhinavGauniyal 5-Aug-13 9:17am    
Yeah, i understand it now, Thanks!

If you are executing the program without arguments then arg[] will be empty and you will get that exception.

Try running like this from the command line:
c:\>yourprogram.exe abhinav

or change your code to:
C#
if(args.length >0)
        Console.WriteLine("Hello, {0}!", args[0]);
else
        Console.WriteLine("Please supply an argument when executing.");
 
Share this answer
 
Comments
ridoy 4-Aug-13 15:53pm    
my 5.
Mehdi Gholam 4-Aug-13 15:58pm    
Thanks ridoy!
ridoy 4-Aug-13 16:05pm    
wc,:)
Maciej Los 4-Aug-13 15:58pm    
+5!
Mehdi Gholam 4-Aug-13 16:07pm    
Thanks Maciej!
 
Share this answer
 
Comments
Mehdi Gholam 4-Aug-13 16:07pm    
5'ed
Maciej Los 4-Aug-13 16:23pm    
Thank you, Mehdi ;)
Sergey Alexandrovich Kryukov 4-Aug-13 17:31pm    
5ed.
—SA
Maciej Los 4-Aug-13 17:32pm    
Thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900