4

I am doing sanitaion on my command line application written in C#. I was wondering if I need to perform a null check against the passed in string[] args array? E.g.:

static int Main(string[] args)
{         
    if(args != null) { // is this needed?

    }
}

Please note I have found similar question concerning Java, but was unable to find anything concerning the command line arguments in C# (& .NET in general).

Also note that I have indeed tried to pass no arguments to my command line application and have never managed to make the args array object to be null. I have also tried accessing the command line arguments using the Environment.GetCommandLineArgs() utility function, and that was also never null.

I have also read this guide written by MS, and couldn't see any explicit guarantee that the args array will never be null.

Edit: Simplified my example.

Evdzhan Mustafa
  • 3,645
  • 1
  • 24
  • 40
  • As a best practice (and even for unit testing), it's best to check for `null` anyway – DiskJunky Sep 26 '17 at 09:58
  • This is fairly opinion-based. In my honest opinion you should *allways* check arguments within a **public API**. – MakePeaceGreatAgain Sep 26 '17 at 10:03
  • If you have to pass arguments, then you **can** pass `null`. That makes `null` check reasonable and tbh vital. – Sinatr Sep 26 '17 at 10:05
  • The edit makes question completely different and clearly [duplicate](https://stackoverflow.com/q/11791969/1997232). If you check [msdn](https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx) you will read why it can not be `null`. – Sinatr Sep 26 '17 at 10:13
  • Checking for null where null is not expected makes no sense. If somebody else reads your code he will first wonder what is the use-case behind that check. – mihail Sep 26 '17 at 10:18
  • @mihail this is not obscure, just defensive programming. If I read a reference type variable checked for null, even if it is supposed to be always non-null, I never complain – ccalboni Sep 26 '17 at 10:25
  • @ccalboni it's the same to me as catching an exception and do nothing with it. Defensive programming? More like bunch of unnecessary checks that makes the code harder to understand. – mihail Sep 26 '17 at 10:55
  • 1
    Calling the entrypoint using reflection would be a possiblity for `args` to be `null` see: https://dotnetfiddle.net/70zCd1. – thehennyy Sep 26 '17 at 11:12
  • @mihail I can't see how a possible error that prevents your program from even begin doing something compares with an unuseful caught exception, which is just bad behavior, not defensiveness IMHO. If you think that a `variable != null` makes code harder to understand maybe you have to look into some linq queries I have seen... – ccalboni Sep 26 '17 at 11:42
  • @ccalboni checking for null at that exact point is pointless. If you somehow end up calling the main method with null args (as thehennyy mentioned via reflection), you're doing something wrong in general and that could lead to unexpected behavior in the further execution. Throwing exception (even NPE) is good enough in this case. – mihail Sep 26 '17 at 11:55
  • @thehennyy you can even call it on your own - `Main(null)` - but that doesn't mean it's right thing to do or it's a good practice. – mihail Sep 26 '17 at 11:59
  • 1
    @mihail I did not say that you should do that. But as you can see at the provided fiddle, sometimes you are not the first one in the callstack and therefore nothing is wrong in being prepared for all types of possible input. – thehennyy Sep 26 '17 at 12:03
  • @thehennyy being prepared for something outside the language standard? No thanks. – mihail Sep 26 '17 at 12:10

1 Answers1

7

The C# standard addresses this in section 3.1 Application Startup:

The entry point may optionally have one formal parameter. The parameter may have any name, but the type of the parameter must be string[]. If the formal parameter is present, the execution environment creates and passes a string[] argument containing the command-line arguments that were specified when the application was started. The string[] argument is never null, but it may have a length of zero if no command-line arguments were specified.

(My bolding)

So the answer is: No, a console application's Main() can never receive a null args[] parameter.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276