Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

named parameters Console.WriteLine c# [duplicate]

I frequently write a Console.WriteLine statement and then modify it at a later stage. I then end up with statements such as this.

Console.WriteLine("{2} is not greater than {0} or smaller than {1}",min,max, grade);

Is there a way to explicitly name the parameters being passed to the Console.WriteLine, so that I do not need to pass min, max and grade in the order?

As a related note, I often remove variables to be printed in Console.WriteLine, such as below, where {2} has been removed.

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade);

Is there a way to not change the numbering and say something like:

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",{0}:min,{1}:max, {3}: grade);

in a similar way to statements like this

@Html.ActionLink("Your profile", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" });
like image 994
asterixorobelix Avatar asked Jul 24 '26 14:07

asterixorobelix


1 Answers

Instead of this:

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade);

You could also use C# 6 string interpolation syntax:

Console.WriteLine($"{grade} is not greater than {min} or smaller than {max}");
like image 142
Romano Zumbé Avatar answered Jul 27 '26 02:07

Romano Zumbé



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!