All that you should know about console arguments — args[]

Jamshaid Kamran
3 min readAug 15, 2022

--

Starting as a Software Developer, you have to deal with the void Main (args[]) method a lot. The obvious question comes in your mind when you deal with the Main(args[]) method is, what are these args[]? Well, we will discuss Main() method and the arguments that are passed in it.

As it is an established fact that Main(args[]) is the first method that gets triggered and is responsible for the execution of your application’s logic. If you declare the Main method as void Main(args[]) or declare it like void Main() i.e without the arguments, it would be perfectly fine and would still be triggered as the entry point of the application.

Now, the question is, what are these arguments that do not affect the application but still are widely used in the application? To answer that, we need to know the usages of the console applications.

Scenario 1: You have a console application that runs and based on some value executes your logic. For example, you need to print a specific user’s detail on the console screen. To distinguish that specific user from other users, you might want to pass an Id or a Username so your application could either make a database or an API call to fetch that specific user.
Solution: Now, you cannot achieve it with a simple Main() method. From the above scenario, we might want to pass an argument for the Username to pass it on to the respective API or database.

public static void Main(string[] args)
{
// var userDetails = api.FetchUserByUsername(args[0]);
}

With above method, you can get the username or user Id from Console Arguments and perform your desired operations with it.

How to pass the args[]: In order to execute the application you need to open cmd.exe and run the application and pass all your parameters with a space separator. For example:

1. Open cmd.exe.
2. Navigate to the folder where application is.
3. Run the following command as an example:
a. MyConsoleApp.exe "Jamshaid".
4. The application that runs, would have "Jamshaid" as a string on first index of args[] array in Main method.

Similarly, you can pass as many parameters as you can and respective indexes would have the value that you pass. For example:

MyConsoleApp.exe "abc@xyz.com" "remove"

Above example triggers the console application with two arguments (email, action). With the above sample your app can perform remove operation based on the email address provided.

public static void Main(string[] args)
{
var email = args[0];
var action = args[1];
// Perform the desired action on the email address
}

Scenario 2: You want to start an Application which has the detail of the files that were dragged and dropped on to it.

Now, default behavior of the Windows OS as of the time of writing this story is, Windows passes the path of all the selected path into the console arguments so you can read or modify the files that were dragged and dropped onto your application. For Example:

static void Main(string[] args)
{
foreach (var arg in args)
{
Console.WriteLine(arg);
}
Console.ReadLine();
}
Dragging and dropping some files on Test Console application.

The above test application would actually produce the output as:

Response of the console test application after dragging and dropping of files.

There are better ways of accepting the console arguments, which we will discuss in another story.

I hope you have now a better knowledge of these console arguments and whether you want to use those in your application or not.

(https://stackoverflow.com/a/45752359/4308286)

--

--