【问题标题】:named parameters Console.WriteLine c# [duplicate]命名参数Console.WriteLine c# [重复]
【发布时间】:2017-07-27 15:41:42
【问题描述】:

我经常写一个 Console.WriteLine 语句,然后在以后修改它。然后我会得到这样的陈述。

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

有没有办法明确命名传递给 Console.WriteLine 的参数,这样我就不需要按顺序传递 min、max 和grade?​​p>

作为相关说明,我经常删除要在 Console.WriteLine 中打印的变量,如下所示,其中 {2} 已被删除。

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

有没有办法不更改编号并说:

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

类似于这样的陈述

@Html.ActionLink("Your profile", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" });

【问题讨论】:

  • 使用 C# 6 字符串插值:$"{grade} is not greater...".
  • 为什么不使用字符串插值? $"{grade} 不大于 {min} 或小于 {max}"
  • 请注意,字符串中没有自动完成。

标签: c# console.writeline


【解决方案1】:

而不是这个:

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

您也可以使用 C# 6 字符串插值语法:

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

【讨论】:

    【解决方案2】:

    是的。

    你现在可以像这样输入字符串

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

    注意字符串前面的 $ 符号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2011-01-19
      • 2013-04-18
      • 2021-09-15
      • 2019-11-28
      相关资源
      最近更新 更多