Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output all command line parameters

I am working on a console application, that receives a pretty long list of parameters. For debugging purpose I need to print the parameters passed to a an output file. Right now, I am using the following code to concat command line parameters.

static void Main(string[] args)
{
    string Params = string.Empty;
    foreach(string arg in args)
    {
       Params += arg + ",";
    }
}

Is there any better way to accomplish this?


2 Answers

What about

Params = string.Join(",", args);

Your foreach approach is not very performant. Since a string is immutable, that means for each iteration of the loop, the string will get thrown away for garbage collection, and a new string will be generated. In the string.Join case, only one string will be generated.

Inside the loop, to get around the same performance, you will have to use a StringBuilder, but in this case it's really no reason not to use string.Join since the code will be much more readable.

like image 101
Øyvind Bråthen Avatar answered Feb 19 '26 09:02

Øyvind Bråthen


You could use this piece of code

String.Join(", ", Environment.GetCommandLineArgs())
like image 39
AbrahamJP Avatar answered Feb 19 '26 08:02

AbrahamJP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!