【发布时间】:2021-02-15 16:04:05
【问题描述】:
我无法理解为什么这段代码会产生这样的结果:
$ type t.cs
using System;
using static System.Console;
using static System.Environment;
class P
{
public static void Main()
{ uint a=1;
string b="a string";
string c=String.Format ( $"{0}\t{1}" , a , b) ;
Out.WriteLine( c );
Exit(0);
}
}
$ csc t.cs
Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354)
Copyright (C) Microsoft Corporation. All rights reserved.
$ .\T.EXE
0 1
我已阅读有关 String.Format 的文档:
https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0
但我仍然不知道如何解释上面的代码打印的原因
"0
谁能给我指点一下?
【问题讨论】:
-
您将字符串插值与字符串格式混合。只使用其中之一。 string.Format 接收没有任何占位符要替换的字符串,因为插值首先发生
-
这意味着您需要删除格式化字符串前面的
$。
标签: c# windows-10 string.format