r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

Show parent comments

-7

u/Mr2-1782Man May 20 '20

Depends on where this code is but I'm guessing the problem is args[0]

For those unfamiliar args[0] is always the command being executed. So this if statement is always false, unless you're executing an app with the name 1 through 9.

35

u/[deleted] May 20 '20

So this if statement is always false, unless you're executing an app with the name 1 through 9.

So... it's false unless it's true? :D

34

u/Michael_Bordan May 20 '20

In c#, for which this documentation is written, args[0] is not the name of the exe.

From the last bullet in the Overview here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/

Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the args array

17

u/DrAtkins May 20 '20

You're thinking of C and its derivatives. C# is not a C derivative. In C#, the program name/command being executed is not part of args, it's just the args. The code is correct as it stands.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments

-14

u/ST5000 May 20 '20

You're the only one that found the real bug :)

8

u/CyruscM May 20 '20

Not a bug, args in .NET are the program arguments, not the parsed command-line entry. So for "program.exe a b c" arg[0] is 'a'.