5

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" });
asterixorobelix
  • 118
  • 1
  • 7

2 Answers2

9

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}");
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
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.

Rob Anthony
  • 1,743
  • 1
  • 13
  • 17