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
Command Line arguments in C#
Command line arguments in C# allow you to pass data to your program when it starts. These arguments are received through the string[] args parameter in the Main method, making your applications flexible and configurable from the command line.
When you create a C# program, the Main method can accept command line arguments as an array of strings. Each argument passed from the command line becomes an element in this array.
Syntax
Following is the syntax for accepting command line arguments in the Main method −
static void Main(string[] args) {
// args contains command line arguments
}
To run a program with command line arguments −
program.exe arg1 arg2 arg3
Basic Command Line Arguments Example
Here's how to access and display command line arguments −
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Length of the arguments: " + args.Length);
Console.WriteLine("Arguments:");
foreach (string arg in args) {
Console.WriteLine(arg);
}
}
}
If you run this program with arguments like program.exe Hello World 123, the output would be −
Length of the arguments: 3 Arguments: Hello World 123
Using Command Line Arguments for Calculations
Command line arguments are often used to pass numeric values for calculations −
using System;
class Calculator {
static void Main(string[] args) {
if (args.Length < 2) {
Console.WriteLine("Please provide two numbers as arguments.");
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("Invalid number format. Please enter valid numbers.");
}
}
}
If you run this with program.exe 15.5 8.2, the output would be −
Number 1: 15.5 Number 2: 8.2 Sum: 23.7 Product: 127.1
Processing Named Arguments
You can also process named arguments by checking for specific patterns −
using System;
class NamedArgsDemo {
static void Main(string[] args) {
string name = "Guest";
int age = 0;
for (int i = 0; i < args.Length; i++) {
if (args[i] == "-name" && i + 1 < args.Length) {
name = args[i + 1];
i++; // Skip the next argument since it's the value
}
else if (args[i] == "-age" && i + 1 < args.Length) {
if (int.TryParse(args[i + 1], out age)) {
i++; // Skip the next argument
}
}
}
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");
}
}
}
If you run this with program.exe -name Alice -age 25, the output would be −
Name: Alice Age: 25 Status: Adult
Key Points
Command line arguments are always strings ? convert them to other types as needed.
The
args.Lengthproperty tells you how many arguments were passed.Arguments are separated by spaces in the command line.
Always validate arguments before using them to avoid runtime errors.
Use
try-catchblocks when converting arguments to other data types.
Conclusion
Command line arguments in C# provide a powerful way to make your programs configurable and interactive. They allow users to pass data directly when starting the program, making applications more flexible and suitable for automation and scripting scenarios.
