net-core
  1. net-core-command-line-arguments

Command-line arguments - (.NET Core Console Applications)

In .NET Core console applications, command-line arguments are the parameters that are passed to the application when it is executed from the command line. These arguments are useful for receiving user input or for controlling the behavior of the application.

Syntax

Command-line arguments are passed to a console application as a series of strings, separated by spaces. These arguments can be accessed through the args parameter in the Main method:

static void Main(string[] args)
{
    // Code block
}

Here, the args parameter is an array of strings, where each element contains a single argument passed to the application.

Example

Here's an example of how to use command-line arguments in a .NET Core console application:

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to my application!");

            if (args.Length == 0)
            {
                Console.WriteLine("Please enter some arguments.");
            }
            else
            {
                Console.WriteLine($"You entered {args.Length} arguments:");
                for (int i = 0; i < args.Length; i++)
                {
                    Console.WriteLine($"{i}. {args[i]}");
                }
            }
        }
    }
}

In this example, the console application prints a welcome message and then checks whether any command-line arguments were passed to it. If no arguments were passed, the application prompts the user to enter some. If arguments were passed, the application outputs the number of arguments and each individual argument.

Output

When you run the application with command-line arguments, the output will vary based on the number of arguments passed in and the values of those arguments. The application will output the list of arguments provided by the user.

Explanation

Command-line arguments provide a way to configure an application without changing its source code. Instead, information can be supplied via the command line when the application is invoked. This is especially useful for console applications that don't have a user interface.

Use

Command-line arguments can be used to provide user input to a console application, or to control the behavior of the application. For example, an application that processes files might use a command-line argument to specify the path to the file that should be processed.

Important Points

  • Command-line arguments are passed to a console application as an array of strings.
  • Command-line arguments allow you to configure an application without changing its source code.
  • Command-line arguments can be used to provide user input to a console application or to control the behavior of the application.

Summary

In this page, we discussed command-line arguments in .NET Core console applications. We explained the syntax, provided an example, and explained the output, explanation, use, and important points. Command-line arguments provide a flexible way to configure and control console applications.

Published on: