4

I'm a beginner in learning programming. I ask about using the string array in main method as a parameter. Why not writing the Main() method without the string array? What is the point of having this array in Main method?

For example:

public static void Main(string[] args) 
{
Console.WriteLine(args.Length);
Console.WriteLine(args[1]);
}
3

3 Answers 3

19

Console applications predate GUI applications, and these take command-line parameters for very long time (at least from CP/M time, which preceded MS-DOS, which preceded Windows, which preceded Windows NT — all preserving more or less the same logic).

You can pass command-line parameters to Windows GUI apps, too.

These are the args.

8
  • 3
    This is essentially it. The underlying system is almost always something like a command line. I don't know of any systems that don't let you pass arguments in this way. Windows shortcuts, for instance, specifically allow this.
    – Magus
    Dec 8, 2014 at 23:28
  • In addition to adding the ability to open files using a flag you can also add support to enable debug/logging for tests or pipe data in/out of an application if necessary. Dec 15, 2014 at 6:09
  • Actually this is older than that. Unix already uses it in C, with its int main(int argc, char * argv[]).
    – mouviciel
    Dec 16, 2014 at 8:45
  • @mouviciel: Obviously it is older than that (and probably older than Unix). The original question is tagged with c#, so it's [mostly] about Windows, and Windows's lineage starts with CP/M and does not cross with Unix.
    – 9000
    Dec 16, 2014 at 15:22
  • @Craig: I'm aware of the NT heritage; inside, Windows is a much nicer OS that one might think looking at the various 'surface-level' APIs. But this applies to the kernel architecture, not (so much) the userland. Obviously, MS had to support the previous convention of invoking things like foo /h that people were used to.
    – 9000
    Mar 28, 2015 at 20:06
7

This is not the sole province of "old" console (command line) applications. Every running program has a "command line" that points at the executable image and includes any command line parameters. Plenty of GUI apps take arguments that alter either their initialization behavior or runtime behavior or both.

Most languages do let you specify an entry-point function that takes no arguments. But virtually all of them also allow you to pass arguments. This args array is simply an ordered collection of whatever is passed on the command line after the name of the executable file when the program is executed. EDIT: And of course in the case of the C language (as one example), the first argument (index 0) is actually the name of the program.

For example, imagine a pointless little program that displays a message in a message box, and lets you specify the message and the title for the window on the command line, like this:

myprogram.exe -title Foo -message Bar

Your args array in this case will look like this, presuming a language like C# which does not include the program name in the args array:

args[ 0 ] = "-title"
args[ 1 ] = "Foo"
args[ 2 ] = "-message"
args[ 3 ] = "Bar"

Making sense of the order of the arguments, and which ones are valid or not, is totally up to your own application code.

3
  • 2
    Almost, but not quite. Generally, args[0] is the full executable path of the program itself. in this case, args[1] would be "-title", etc At least for C, and I assume it to be the same for other languages. See publications.gbdirect.co.uk/c_book/chapter10/… Dec 9, 2014 at 8:17
  • 7
    It's not generally the same for other languages. The question specifically asked about c#, which does not include the executable name in args (which is available via System.Reflection.Assembly.GetExecutingAssembly instead).
    – Jules
    Dec 9, 2014 at 8:52
  • 1
    @Mawg argc in C may be zero. in case of non-zero argc, argv[0] shall be a string that represents the "program name", or an empty string if the name is not available from the host environment. There is no requirement anywhere that it shall be a "full executable path". This is all from C99 section 5.1.2.2.1. Even your non-normative URL says this. Dec 15, 2014 at 11:49
1

Have you ever listed items in a directory using

ls -a ~/Downloads

or

dir C:\Users\user1\Downloads\ /P

Now those "ls" and "dir" are commands/programs and the latter arguments are passed to the programs as string[] argv or sometimes int argc, char** argv. They provide arguments to the programs so they can behave accordingly.

When you start a program the OS loads the program to the memory and pushes the arguments to the stack. Now they are available to the program to be used.

High-level languages give you an easy way to access those arguments on the stack (as string arrays or an array of pointers to null terminated strings). If you used assembly, you would need to do some pointer arithmetic and load the arguments from stack to registers when you needed them.

For more details checkout following link, good explanation: FreeBSD Command Line Arguments

2
  • this seems to merely repeat points made and explained in this and especially this prior answer
    – gnat
    Dec 11, 2014 at 15:36
  • 1
    @gnat Thank you for the feedback, but I think how you say something is also as important as what you say. I made the same point differently. Dec 11, 2014 at 23:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.