Share via


Command-Line Arguments (C# Programming Guide) 

The Main method can use arguments, in which case, it takes one of the following forms:

static int Main(string[] args)
static void Main(string[] args)

The parameter of the Main method is a String array that represents the command-line arguments. Usually you check for the existence of the arguments by testing the Length property, for example:

if (args.Length == 0)
{
    System.Console.WriteLine("Please enter a numeric argument.");
    return 1;
}

You can also convert the string arguments to numeric types by using the Convert class or the Parse method. For example, the following statement converts the string to a long number by using the Parse method on the Int64 class:

long num = Int64.Parse(args[0]);

It is also possible to use the C# type long, which aliases Int64:

long num = long.Parse(args[0]);

You can also use the Convert class method ToInt64 to do the same thing:

long num = Convert.ToInt64(s);

For more information, see Parse and Convert.

Example

In this example, the program takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the program issues a message that explains the correct usage of the program.

// arguments: 3
public class Functions
{
    public static long Factorial(int n)
    {
        if (n < 0) { return -1; }    //error result - undefined
        if (n > 256) { return -2; }  //error result - input is too big

        if (n == 0) { return 1; }

        // Calculate the factorial iteratively rather than recursively:

        long tempResult = 1;
        for (int i = 1; i <= n; i++)
        {
            tempResult *= i;
        }
        return tempResult;
    }
}
class MainClass
{
    static int Main(string[] args)
    {
        // Test if input arguments were supplied:
        if (args.Length == 0)
        {
            System.Console.WriteLine("Please enter a numeric argument.");
            System.Console.WriteLine("Usage: Factorial <num>");
            return 1;
        }

        try
        {
            // Convert the input arguments to numbers:
            int num = int.Parse(args[0]);

            System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num));
            return 0;
        }
        catch (System.FormatException)
        {
            System.Console.WriteLine("Please enter a numeric argument.");
            System.Console.WriteLine("Usage: Factorial <num>");
            return 1;
        }
    }
}

Output

The Factorial of 3 is 6.

Comments

The following are two sample runs of the program assuming that the program name is Factorial.exe.

Run #1:

Enter the following command line:

Factorial 10

You get the following result:

The Factorial of 10 is 3628800.

Run #2:

Enter the following command line:

Factorial

You get the following result:

Please enter a numeric argument.

Usage: Factorial <num>

For more examples on using command-line arguments, see the example in How to: Create and Use C# DLLs (C# Programming Guide).

See Also

Tasks

How to: Display Command Line Arguments (C# Programming Guide)
How to: Access Command-Line Arguments Using foreach (C# Programming Guide)

Reference

Main() and Command Line Arguments (C# Programming Guide)
Main() Return Values (C# Programming Guide)

Concepts

C# Programming Guide