Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How command line arguments are passed in main method in C#?
The Main() method is the entry point of a C# application and can accept command line arguments through its args parameter. These arguments allow users to pass data to the program when it starts, making applications more flexible and configurable.
Syntax
The standard syntax for the Main() method with command line arguments is −
static void Main(string[] args)
The args parameter is a string array that contains all command line arguments passed to the program −
// args[0] = first argument // args[1] = second argument // args.Length = total number of arguments
How Command Line Arguments Work
When you run a program from the command line with arguments, they are automatically parsed and stored in the args array. For example, running myprogram.exe arg1 arg2 arg3 will populate the array as follows −
args[0] = "arg1" args[1] = "arg2" args[2] = "arg3" args.Length = 3
Example - Basic Command Line Arguments
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Total arguments: " + args.Length);
if (args.Length > 0) {
Console.WriteLine("Arguments passed:");
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("Argument " + i + ": " + args[i]);
}
} else {
Console.WriteLine("No arguments were passed.");
}
}
}
If you run this program with arguments like program.exe Hello World 123, the output would be −
Total arguments: 3 Arguments passed: Argument 0: Hello Argument 1: World Argument 2: 123
Example - Processing Numeric Arguments
using System;
class Calculator {
static void Main(string[] args) {
if (args.Length != 2) {
Console.WriteLine("Usage: calculator.exe <number1> <number2>");
return;
}
try {
double num1 = Convert.ToDouble(args[0]);
double num2 = Convert.ToDouble(args[1]);
Console.WriteLine("Number 1: " + num1);
Console.WriteLine("Number 2: " + num2);
Console.WriteLine("Sum: " + (num1 + num2));
Console.WriteLine("Product: " + (num1 * num2));
} catch (FormatException) {
Console.WriteLine("Error: Please enter valid numbers.");
}
}
}
Running this program with calculator.exe 5.5 3.2 would produce −
Number 1: 5.5 Number 2: 3.2 Sum: 8.7 Product: 17.6
Example - Named Arguments
using System;
class NamedArgsDemo {
static void Main(string[] args) {
string name = "Guest";
int age = 0;
for (int i = 0; i < args.Length - 1; i++) {
if (args[i] == "--name" || args[i] == "-n") {
name = args[i + 1];
} else if (args[i] == "--age" || args[i] == "-a") {
int.TryParse(args[i + 1], out age);
}
}
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
if (age >= 18) {
Console.WriteLine("Status: Adult");
} else if (age > 0) {
Console.WriteLine("Status: Minor");
} else {
Console.WriteLine("Status: Age not specified");
}
}
}
Running with program.exe --name John --age 25 would output −
Name: John Age: 25 Status: Adult
Key Points
-
Command line arguments are always received as strings − convert them to other data types as needed.
-
Always check
args.Lengthbefore accessing array elements to avoidIndexOutOfRangeException. -
Arguments containing spaces must be enclosed in quotes when running the program.
-
The program name itself is not included in the
argsarray.
Conclusion
Command line arguments in C# provide a powerful way to make programs configurable and interactive. By accessing the string[] args parameter in the Main() method, you can create flexible applications that accept user input at startup, making them more versatile and user-friendly.
