'How to pass arguments in command line using dotnet?

the command dotnet myapp.dll -- [4, 3, 2] throws the exception System.FormatException: Input string was not in a correct format. I do not know the syntax. How should I pass arguments correctly? I use powershell.



Solution 1:[1]

using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Join('-', args));
        }
    }
}

Call it via Powershell 6:

dotnet .\ConsoleApp3.dll "[1,2,3]"

Output:

[1,2,3]

In the above call, your Main method will receive [1,2,3] as a single string and you have to parse/split it in your code.

If you want an array reflected in the string[] array of Main you can use a PowerShell array:

dotnet .\ConsoleApp3.dll @(1,2,3)

Output:

1-2-3

Here the PowerShell array @(1,2,3) is casted to a string[]-array. Therefore each item of the PowerShell array is injected to the string[] array.

Behavior is the same on PowerShell 5.1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wai Ha Lee