named parameters Console.WriteLine c# [duplicate]
Asked Answered
H

2

7

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" });
Hujsak answered 27/7, 2017 at 7:38 Comment(3)
Use C# 6 string interpolation: $"{grade} is not greater...".Raouf
Why not use string interpolations ? $"{grade} is not greater than {min} or smaller than {max}"Rosary
Note that there is no auto completion in string.Thermolabile
D
11

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}");
Diluent answered 27/7, 2017 at 7:40 Comment(0)
A
0

Yes.

You can now put in strings like this

Console.WriteLine($"{grade} is not greater than {max} or smaller than {min}");

Note the $ sign in front of the string.

Allonge answered 27/7, 2017 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.