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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With