In C#, you can pass command-line arguments to your application and access them within the Main method. The Main method can be defined to accept an array of strings as its parameter, which represents the command-line arguments.
Example of Passing and Accessing Command-line Arguments Here's a simple example demonstrating how to pass and access command-line arguments in a C# console application:
using System;
class Program
{
// Main method accepting command-line arguments
static void Main(string[] args)
{
// Check if arguments are provided
if (args.Length > 0)
{
// Loop through the arguments and print them
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"Argument {i}: {args[i]}");
}
}
else
{
Console.WriteLine("No command-line arguments were provided.");
}
}
}
Running the Application with Command-line Arguments To run the application with command-line arguments, you can use a terminal or command prompt. Navigate to the directory where your compiled executable is located and run it with arguments:
dotnet run arg1 arg2 arg3
Or if you are running the compiled executable directly:
Program.exe arg1 arg2 arg3
Handling Command-line Arguments in Visual Studio
If you are using Visual Studio, you can set command-line arguments in the project properties:
Right-click on the project in Solution Explorer and select "Properties".
Go to the "Debug" tab.
In the "Command line arguments" field, enter the arguments you want to pass to the application.
Save the changes and run the project.
Detailed Explanation
Defining the Main Method: The Main method is defined with a parameter args, which is an array of strings (string[] args). This array holds the command-line arguments passed to the application.
Checking for Arguments: The code checks if any arguments are provided by checking the length of the args array (args.Length).
Looping Through the Arguments: If arguments are provided, a for loop is used to iterate through the args array and print each argument along with its index.
No Arguments Provided: If no arguments are provided, a message is printed indicating that no command-line arguments were given.
Output :
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, you can pass command-line arguments to your application and access them within the Main method. The Main method can be defined to accept an array of strings as its parameter, which represents the command-line arguments.
Example of Passing and Accessing Command-line Arguments
Here's a simple example demonstrating how to pass and access command-line arguments in a C# console application:
Running the Application with Command-line Arguments
To run the application with command-line arguments, you can use a terminal or command prompt. Navigate to the directory where your compiled executable is located and run it with arguments:
Or if you are running the compiled executable directly:
Handling Command-line Arguments in Visual Studio
If you are using Visual Studio, you can set command-line arguments in the project properties:
Detailed Explanation
Output :