我需要使用字符串插值格式化变量,格式字符串是另一个变量:
这是我的示例代码:
static void Main(string[] args)
{
int i = 12345;
Console.WriteLine($"Test 1: {i:N5}");
var formatString = "N5";
Console.WriteLine($"Test 2: {i:formatString}");
}
Run Code Online (Sandbox Code Playgroud)
测试1工作,测试2不起作用.
测试2的确切语法是什么?
简单测试项目:xUnit,针对Net 6.0 环境:
添加System.Runtime的假程序集,编译错误:
1>------ Rebuild All started: Project: TestProject1, Configuration: Debug Any CPU ------
Restored D:\TestProject1\TestProject1\TestProject1.csproj (in 570 ms).
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,16): error CS1503: Argument 1: cannot convert from 'string' to 'System.Runtime.Serialization.SerializationInfo' [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,31): error CS1615: Argument 2 may not be passed with the 'ref' keyword [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>GENERATEFAKES : error : project compilation failed with exit code 1
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,16): error CS1503: Argument 1: cannot …Run Code Online (Sandbox Code Playgroud)