对于这个字符串 $"{1:5d}",C# 中的内插字符串中的“:”有什么作用?

What does the ":" do in the interpolated string in C#, for this string $"{1:5d}"?

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine ($"{1:5d}");
   }
}
// The example displays the following output:
// 5d

:”在内插字符串中的作用是什么? 我知道如果我写,

$"{1:d5}"

我会得到,

00001

但是我没有得到 error/warning 意味着它必须意味着我不知道的东西。

仅供参考,我使用的是 C#7。

它是 format string. It allows you to specfy formatting relevant to the type of value on the left. In first case it tries to apply custom format, but since there is no placeholder for actual value - you get only the "value" of the format (try something like Console.WriteLine ($"{1:00000SomeStringAppended}"); for example, "5d" has the same meaning as "SomeStringAppended" in my example). The second one - d5 is a standart decimal 格式说明符的分隔符,因此您会得到包含格式化值的相应输出。

以下两行将给出相同的结果 - 00001。

var i = $"{1:d5}";
var j = string.Format("{0:d5}", 1);

大括号中的冒号 : 是字符串格式。 您可以阅读有关字符串格式的更多信息 here and about string formatting in a string interpolation here