Command Line Arguments in C#

Command Line Arguments in C# with Examples

In this article, I am going to discuss Command Line Arguments in C# with Examples. Please read our previous article where we discussed Input and Output in C# with Examples.

Command Line Arguments in C#:

We know that we can pass the parameter to a function as an argument but what about the Main(string[] args) method? Can we pass parameters to the Program class Main() method in C#? Yes, we can parameter(s) to the Main() method in C# and this is possible through Command Line Arguments. The arguments which are passed by the user or programmer to the Main() method are termed as Command-Line Arguments in C#.

The Main() method is the starting point from where the program execution starts. The most important point that you need to remember is that the main method doesn’t accept any parameter from any method. It only accepts parameters through the Command-Line. If you notice the Main method signature, it has a string array type parameter that can accept n number of parameters at runtime. In Main(string[] args), args is a string type of array that can hold numerous parameters.

Passing Command Line Arguments in C# using Visual Studio:

Create a new console application and then modify the Program.cs class file as follows:

using System;
namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"First Command Line Argument {args[0]}");
            Console.WriteLine($"Second Command Line Argument {args[1]}");
            Console.WriteLine($"Thrird Command Line Argument {args[2]}");

            Console.ReadLine();
        }
    }
}

If you notice, the above example excepts at least three parameters to be supplied by the Main method. Now, if you run the application, then you will get the following System.IndexOutOfRangeException: ‘Index was outside the bounds of the array’ run time exception.

Passing Command Line Arguments in C# using Visual Studio

And this makes sense. Because we have not supplied any parameters and in the program, the string array does not have an element and we are trying to access the array elements. Now, the question is how we can pass arguments to the main method? The answer is by using the command line. Let us see how we can do this using Visual Studio.

Passing Command Line Arguments in Visual Studio:

Open the Properties window. To open the properties window, right-click on the project in solution explorer and then click on the Properties menu as shown in the below image.

Passing Command Line Arguments in Visual Studio

From the Properties window, select the debug tab and in the Command Line Arguments text box, provide the values that you want to pass to the Main method separated by a space. As in our example, we except three values in the string array, so here I am putting three values Command Line Arguments text box as shown in the below image.

Command Line Arguments in C# with Examples

Here Value1 will store in args[0], Value2 will store in args[1], and Value3 will store in args[2]. Now, save the changes and run the application and you will get the following output in the Console window.

Passing Command Line Arguments in C# using Visual Studio

Important Points:
  1. They are captured into the String args of the main method.
  2. Command Line Arguments can be used to specify configuration information while launching your application.
  3. Information is passed as Strings.
  4. There is no restriction on the number of java command line arguments.
Passing Numeric Command Line Arguments in C#

In C#, arguments are always stored as strings and always separated by white spaces. The main() method of every C# program only accepts string arguments. If an application needs to support a numeric command-line argument, it must convert a string argument that represents a number, such as “34”, to a numeric value. Hence it is possible to pass numeric arguments through the command line. However, we can later convert string arguments into numeric values.

Example to Pass Numeric Command Line Arguments in C#
using System;
namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            //convert into integer type
            int argument1 = Convert.ToInt32(args[0]);
            Console.WriteLine("Argument in Integer Form : " + argument1);

            //convert into double type
            double argument2 = Convert.ToDouble(args[1]);
            Console.WriteLine("Argument in Double Form : " + argument2);
            
            Console.ReadLine();
        }
    }
}

Now, modify the Properties=>Debug window as shown in the below image.

Passing Numeric Command Line Arguments in C#

Now, save the changes and run the application and you will get the following output.

Example to Pass Numeric Command Line Arguments in C#

What happens if the value is not converted to the specified type?

If the arguments cannot be converted into the specified numeric value, then we will get the System.FormatException: ‘Input string was not in a correct format.’

Let us change the command line arguments values as shown in the below image. Here the second argument is of string type which cannot be converted to double.

What happens if the value is not converted to the specified type?

Now save the changes and run the application and you will get the following runtime error.

Command Line Arguments in C# with Examples

In the next article, I am going to discuss Strings in C# with Examples. Here, in this article, I try to explain Command Line Arguments in C# with Examples. I hope you enjoy this Command-Line Arguments in C# with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.