Command-line arguments in a C# console application

Command-line arguments are a useful way to gather input for our console application. In this post, I’ll show how to use these types of arguments in your C# console applications.

Why use command-line arguments?

As you should know by now, algorithms solve a problem by taking input and calculating an output.

For instance, you can have an algorithm that takes a number as input and returns if the number is odd or not as output.

Because in this case the algorithm uses only one number as input, and won’t ask the user for another number, this input can be a command-line argument.

However, if you want to provide a C# console menu for the user, then maybe you shouldn’t use command-line arguments.

However, several console applications and services use command-line arguments to define execution modes. These modes of execution can also be stated using environment variables.

As you can see, the use of command-line arguments depends on the problem you are trying to solve. But one can say that you can use them anytime you need to pass a parameter to an application and, the value on that parameter will be used while the application is running.

Capturing command-line arguments in a C# Console Application

A C# console app application used to be a class with a method that has a specific signature.

See the example below.

using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

The previous example shows a simple console application. The arguments of the method Main, are the ones that are passed as command-line arguments. Notice the method Main is the entry point to the execution of your C# console application.

The parameter args is an array of strings that contains all the command-line arguments of the C# console application.

Let’s say we are expecting two command-line arguments, an integer number and a text (in that order).  We can capture the two values as follows:

using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 2)
 {
                  int number;

                  bool is_int = false;

                 is_int = int.TryParse(args[0], out number);

                 string text = args[1];
                 Console.WriteLine(text);
            }
     }
   }
}

Let’s suppose that our program should print the text several times. The number of times we should print the text is specified in the integer number received as a command-line argument. We can write that code as follows:

using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 2)
         {
                  int number;

                  bool is_int = false;

                 is_int = int.TryParse(args[0], out number);

                 string text = args[1];
                 if (is_int)
                {
                     for (int i = 0; i < number; i++)
                     {
                        Console.WriteLine(text);
                     }
                }
  

            }
     }
   }
}

Notice that we must check if the first command-line argument is an integer to avoid crashes in our console application. We always want to give the best user experience possible.

Starting with .NET 6, C# console applications don’t need to have the code for the main class. Instead, you can write your code straight in an empty file, this simplifies the code of a console app. Source.

Our previous example will be as follows:

if (args.Length == 2)
{
    int number;

    bool is_int = false;

    is_int = int.TryParse(args[0], out number);

    string text = args[1];
    

    if (is_int)
    {
        for (int i = 0; i < number; i++)
        {
            Console.WriteLine(text);

        }
    }
}

Execution of a console application with command-line arguments

There two ways of executing a console application using command-line arguments.

The first and most used is to execute a command in a terminal.

To execute the application above, you must open a terminal in the same folder that your executable file is. When programming in C#, you will find a folder named “bin” in the same folder you created your project. Inside that folder, you will find an executable file.

You can check if you are in the right folder by using the ‘ls’ command if you are in a Unix-based operating system, or the ‘dir’ command if you are using windows.

The name of the application in the example above is ‘ComputingLearner’.  You can have a different file name, depending on what name you used when you created the project.

Now I must enter the following command:

  • ./ComputingLearner 3 “Hi Computing Learner”

From the above command, you should notice I have a Unix-based system, if you use Windows OS, then the command should look like “ComputingLearner.exe 3 “Hi Computing Learner”

When you write that command and press enter, you should see the following output.

command-line arguments example of execution
Command-line arguments example of execution

Command-line arguments using Visual Studio

If you are using an IDE to write a program in C#, the logic choice will be Visual Studio.

In Visual Studio, right-click on the project name (not the solution), and you should see something like the picture below.

command-line arguments in visual studio example
Command-line arguments in visual studio example

Then, click on properties (blue color) and you will see the project property window.

command-line arguments in visual studio project properties window
Command-line arguments in visual studio project properties window

Once in the project property window, you should look on the left side for Run – Configuration – Default. On the right side, you will see a place where you can write the arguments.

Each argument is identified by spaces. If you want to use a string with more than one word as a parameter, you must write the string between quotation marks, as in the example.

The program will handle all the arguments as strings, so you need to convert them to numbers in case you want to use numbers, as in the example above.

If you execute the example above with the same parameters as in the picture, you should get the following result after the execution of the console application.

command-line arguments example of execution c# visual studio
Command-line arguments example of execution c# visual studio

H@ppy coding!

Related posts: