Click here to Skip to main content
15,302,455 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
using System;

namespace commandline
    class Program
        public static void Main(string[] args)
            Console.Write("wellcome to");
            Console.Write(" " + args[0]);
            Console.WriteLine(" " + args[1]);

[edit]Code block added - OriginalGriff[/edit]
Updated 4-Mar-11 2:50am

I think the error message is self explanatory:

You are trying to read the command line arguments, which are surely not passed.
Manfred Rudolf Bihy 4-Mar-11 8:40am
Spot on! 5+
Manas Bhardwaj 4-Mar-11 8:44am
if(args.Length >=2)
    Console.Write(" " + args[0]);
    Console.WriteLine(" " + args[1]);
    Console.WriteLine(" Less then 2 command line arguments");
Manfred Rudolf Bihy 4-Mar-11 8:39am
It's always a good idea to check first! 5+
rickysharp 4-Mar-11 11:08am
heloo.. thank you for your reply ... in this code i could not able to pass arguments
can you help with that?
I refer you to the answer I gave to your exact same question in the C# forum nearly five hours ago...[^]
johannesnestler 4-Mar-11 8:40am
@rickysharp: if you get an "Index Out Of Range" Exception - what could be the problem? Maybe an there is an INDEX out of range (=there is no element in the sequence/array with the specified index) - look at OriginalGriff's link - he showed you how to check the index first! Nothing against you, but I'm just wondering what you would have done in the past, when such nice error messages looked like that -> SYSTEMERROR
Manfred Rudolf Bihy 4-Mar-11 8:44am
You're barking up the wrong tree. This comment should be on OP's question and not on Griff's solution. :)
OriginalGriff 4-Mar-11 8:45am
I know what you mean: When a DOS program exited with error code 2 it was much more fun trying to work out why!
(2 = "File not found", BTW. Which file? Hmmm.)
Manfred Rudolf Bihy 4-Mar-11 8:45am
Looks as if OP is learning resistant. +5 :)
OriginalGriff 4-Mar-11 8:46am
Ooooo! I like that: mind if I pinch it for a sig?
Manfred Rudolf Bihy 4-Mar-11 8:51am
Go ahead and be my guest! :-)
You must only be passing one command line argument to the program (or calling it without any argments). Unlike C and C++, where args[0] is the program name and args[1] is the first parameter, C# only puts the command line arguments into the array
Manfred Rudolf Bihy 4-Mar-11 8:38am
Correct! 5+
for (int i = 0 ; i<argc ; i++ )
Console.Write(argv[i]+" ");
Manfred Rudolf Bihy 4-Mar-11 8:42am
What is this supposed to be?
Piccadilly Yum Yum 4-Mar-11 8:44am
echo of command line with arguments without index exception
Manfred Rudolf Bihy 4-Mar-11 8:56am
Ok! After you edit it looks a little better, but only a little. Where did you take argc from now? If you really want to output the command line arguments (if there are any) why not like this.
foreach(String arg in args)
Console.Write(String.Format("{0} ", arg));

How's that?
Graham Shanks 4-Mar-11 8:56am
Well, argc is not available in C#, you would use args.Length. Also using foreach would be better C# code
Manfred Rudolf Bihy 4-Mar-11 8:57am
Looks like your read my mind. :)
Toli Cuturicu 4-Mar-11 9:17am
Strange code... there is neither argc, nor argv there! It will surely not compile!
Piccadilly Yum Yum 4-Mar-11 9:19am
Karthik. A 4-Mar-11 9:42am
this is called the "C" (language) effect ;)

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