Pass Arguments To a C# Application From The Command-Line

Passing arguments to a C# .NET application from the command line can help pass values and variables and process information.

By default, the main function has a built-in option to accept arguments without modifying or writing extra code.

The following code example shows a program that accepts arguments via the command line and prints them out.

using System;
namespace passargs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"You passed {args.Length} arguments");
            foreach(string count in args)
            {
             Console.WriteLine(count);
            }
        }
    }
}

Pass Arguments

To pass arguments to the command, I am using the dotnet run command with the arguments as shown below.

dotnet run green blue yellow

To pass arguments to a published app run this line.

dotnet passargs.dll blue

The output is shown below.

You passed 3 arguments
green
blue
yellow

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.