9

How exactly is string[] args populated in a C# Main method?

For instance, is white space stripped? Are any of the elements ever empty string or null? How are single and double quotes handled?

MSDN doesn't explain how and merely says

The parameter of the Main method is a String array that represents the command-line arguments

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • 3
    "For instance, is white space stripped?" --- why don't you try? It would take 1 minute to do. – zerkms Sep 21 '12 at 09:31
  • There's some information here, although whitespace doesn't seem to make an appearance in this documentation. http://msdn.microsoft.com/en-us/library/acy3edy3.aspx – ta.speot.is Sep 21 '12 at 09:34
  • Another interesting read: http://stackoverflow.com/questions/6605956/what-happens-when-a-net-application-is-started – Adam Houldsworth Sep 21 '12 at 09:36
  • 3
    The rules (well at least concerning escaping of quotes - which is the hardest thing), are explained in the Remarks of the [Environment.GetCommandLineArgs() function](http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx). – Christian.K Sep 21 '12 at 09:36
  • 4
    I guess what you are really asking is: "What algorithm is used to split a command-line, which is basically one long string, into the C# args array?". This is actually a good question, so +1 to counter the downvotes. – Heinzi Sep 21 '12 at 09:37
  • Thank you Christian! That article is great, it has examples and everything. – Colonel Panic Sep 21 '12 at 09:40
  • The link you provide has another link within it [How to: Display Command Line Arguments](https://msdn.microsoft.com/en-us/library/96s74eb0.aspx) which does state: White-space between arguments is removed. – Rick Davin Feb 27 '15 at 17:03

3 Answers3

5

When you start a process, you can pass a string as your argument. How those are arranged and split up is entirely up to you.

So if using the Windows command line, you ran:

myexe.exe "Hello World" Joe Bloggs

Your array would contain:

{"Hello World", "Joe", "Bloggs"}

But it's only split up in that particular way (notice the quotes around Hello World are removed) because the .Net framework is automatically parsing it for you.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • 1
    It depends on the command-line outside?! Okay, that's interesting. – Colonel Panic Sep 21 '12 at 09:38
  • 1
    I don't think you can pass in an array of strings in windows. You can pass in a single string, and the splitting is up to the called process. There are conventions for how to split it, but they're just that. The callee can choose to use different rules. | I believe this is different on unixoid systems, but I'm no expert on those. – CodesInChaos Sep 21 '12 at 09:42
  • I'm pretty sure Windows uses argc + argv same as *nix does. – PhonicUK Sep 21 '12 at 10:00
  • The calling process does not specify a `string[]` as arguments. It only specifies a `string` (see Win32 `CreateProcess` API). The .NET runtime is interpreting that string so that you get a nice `string[]`. – usr Sep 21 '12 at 10:09
  • @PhonicUK, Where is the documentation for this? – Ryan Griffith Jan 16 '14 at 00:28
4

I believe the args given to Main are those returned by Environment.GetCommandLineArgs() after removing the first of the list. MSDN describes the suprisingly complex logic concerning backslashes:

Command line arguments are delimited by spaces. You can use double quotation marks (") to include spaces within an argument. The single quotation mark ('), however, does not provide this functionality.

If a double quotation mark follows two or an even number of backslashes, each proceeding backslash pair is replaced with one backslash and the double quotation mark is removed. If a double quotation mark follows an odd number of backslashes, including just one, each preceding pair is replaced with one backslash and the remaining backslash is removed; however, in this case the double quotation mark is not removed.

Thanks to Christian.K in the comments.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
0

The parameters passed to your program are operating-system dependent.

You should test arguments for nulls, empty strings, strip white-space, and handle single/double quotes within your program (as necessary).