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]);
}
-
cause that's how everyone else does it (except win32 winMain for some reason)ratchet freak– ratchet freak2014年12月08日 22:32:48 +00:00Commented Dec 8, 2014 at 22:32
-
As you can add multiple parameters thereVsevolod Goloviznin– Vsevolod Goloviznin2014年12月08日 22:35:28 +00:00Commented Dec 8, 2014 at 22:35
-
See publications.gbdirect.co.uk/c_book/chapter10/…Mawg– Mawg2014年12月09日 08:18:21 +00:00Commented Dec 9, 2014 at 8:18
3 Answers 3
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
.
-
3This 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– Magus2014年12月08日 23:28:21 +00:00Commented 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.Evan Plaice– Evan Plaice2014年12月15日 06:09:04 +00:00Commented 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– mouviciel2014年12月16日 08:45:15 +00:00Commented 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– 90002014年12月16日 15:22:41 +00:00Commented 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– 90002015年03月28日 20:06:22 +00:00Commented Mar 28, 2015 at 20:06
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.
-
2Almost, 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/…Mawg– Mawg2014年12月09日 08:17:55 +00:00Commented Dec 9, 2014 at 8:17
-
7It'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– Jules2014年12月09日 08:52:07 +00:00Commented Dec 9, 2014 at 8:52
-
1@Mawg
argc
in C may be zero. in case of non-zeroargc
,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.Lars Viklund– Lars Viklund2014年12月15日 11:49:49 +00:00Commented Dec 15, 2014 at 11:49
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
-
-
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.Mert Akcakaya– Mert Akcakaya2014年12月11日 23:48:02 +00:00Commented Dec 11, 2014 at 23:48