34

I have this code:

public static void Main(string[] args)
{         
    if (string.IsNullOrEmpty(args[0]))  // Warning : Index was out of the bounds of the array
    {
        ComputeNoParam cptern = new ComputeNoParam();
        cptern.ComputeWithoutParameters();
    }
    else
    {
        ComputeParam cpter = new ComputeParam();
        foreach (string s in args){...}
    }
}

Also tried if(args.Length==0), but it still doesn't work.

Basically I want to find out if the user called the program with arguments. If not the program will ask for input.

How can I do this? Thanks in advance.

radbyx
  • 9,352
  • 21
  • 84
  • 127
robertpas
  • 643
  • 5
  • 12
  • 25
  • 3
    What happens when you try `args.Length == 0`? – Richard Ev Aug 03 '12 at 08:23
  • 2
    What didn't work with `args.Length == 0`? A humble tip for the future: When you say "it doesn't work", that's not nearly enough of an issue-explanation. Elaborate, say what doesn't work, how it doesn't work, what happens instead that you don't expect, etc. =) – J. Steen Aug 03 '12 at 08:23
  • When you use `if (args.Length == 0)`, and it doesn't work, what does it do instead of working? – phoog Aug 03 '12 at 08:23
  • Scratch that, it works. There was a problem in the ComputeNoParam class – robertpas Aug 03 '12 at 08:26

6 Answers6

57

if(args.Length==0) should work, args[0] requires at least one argument to not crash.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
11
if(args == null || args.Length == 0)
{
    // no arguments
}
else
{
    // arguments
}
Tom
  • 4,096
  • 2
  • 24
  • 38
  • 2
    I tried to launch service without any arguments. Args was an 0 element array, so I guess args will never be null? – Maurice Klimek Jun 05 '19 at 05:27
  • 1
    yeah, I guess so – Tom Jun 05 '19 at 06:18
  • 3
    [Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) explicitly states *the `args` array cannot be null* – Caius Jard May 07 '21 at 09:49
5

it's an array and there's two scenarios that might have the meaning NO arguments passed. Depending on your semantics

args == null or args.Length == 0

In this case where the method is called when the program is executed (e.g. not calling the method as part of say a unit test) the args argument will never be null (making the first test redundant) I've included it for completeness because the same situation might easily be encountered in other methods than main

if you test them in that order you don't have to worry about args being null in the latter expression

if(args == null || args.Length == 0){
    ComputeNoParam cptern = new ComputeNoParam();
    cptern.ComputeWithoutParameters();
}
else
{
    ComputeParam cpter = new ComputeParam();
    foreach (string s in args){...}
}
Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • Should be answer. This is more complete than the posts by Tom or Albin, even though this was answered a few minutes later. – JoePC Jul 19 '19 at 16:01
  • 1
    [Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) explicitly states *the `args` array cannot be null* – Caius Jard May 07 '21 at 09:49
  • THat is only true if it's call by the runtime as the sntry point. The method might be call in other manners as well where args might be null which I also state in the answer – Rune FS May 08 '21 at 12:00
4

This should also work:

if (args.Length < 1)
{
    //no args passed
}
Michelle
  • 41
  • 1
3

This should work on your scenario:

if (args == null || args.Length == 0)
{
    //Code when no arguments are supplied
}
else
{
    //Code when arguments are supplied
}

Notice how check args == null should be executed before args.Length == 0 when using || or &&. This is called "Condition Short-Circuiting" where C# will start evaluating the first condition and if it's true, will not look at the second condition. In this scenario, C# will evaluate the second condition only if the first condition is false.

Suppose if your conditions are aligned as if(args.Length == 0 || args == null) and args become null, it will throw an exception on the first condition, although the second condition is true.

This is something we need to keep in mind when placing conditions.

  • 1
    Will args ever be null in any case in main? – Vivek MVK Dec 20 '19 at 16:33
  • 2
    [Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) explicitly states *the `args` array cannot be null* – Caius Jard May 07 '21 at 09:50
1

Another available option if you're already using System.Linq is to make use of the Any() extension, for instance:

public static void Main(string[] args)
{
    if (args == null && !args.Any())
    {
        // No parameters passed.
        ComputeNoParam cptern = new ComputeNoParam();
        cptern.ComputeWithoutParameters();

        return;
    }

    // process parameters
    ComputeParam cpter = new ComputeParam();
    foreach (string s in args){...}
}

This could also be written:

public static void Main(string[] args)
{
    if (!args?.Any() ?? true)
    {
        // No parameters passed.
        ComputeNoParam cptern = new ComputeNoParam();
        cptern.ComputeWithoutParameters();

        return;
    }

    // process parameters
    ComputeParam cpter = new ComputeParam();
    foreach (string s in args){...}
}

This just shows another option available to you, I'd agree with going with .Length, although I would drop the null check and use conditional access instead, so.

if (args?.Length == 0) {
    // Code hit if args is null or zero
}
derpasaurus
  • 397
  • 3
  • 13
  • 2
    [Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments) explicitly states *the `args` array cannot be null* – Caius Jard May 07 '21 at 09:50