C#代码中的美元符号是什么意思?

use*_*707 4 c# c#-6.0

今天,我从我的客户端提取代码,我在这一行中收到错误.

throw new Exception($"One or more errors occurred during removal of the company:{Environment.NewLine}{Environment.NewLine}{exc.Message}");
Run Code Online (Sandbox Code Playgroud)

这条线也

moreCompanies = $"{moreCompanies},{databaseName}";
Run Code Online (Sandbox Code Playgroud)

$符号对我来说太奇怪了.这是C#代码.

Ale*_*ker 10

该$部分告诉编译器您需要插值字符串.

插值字符串是C#6.0的新功能之一.它们允许您使用相应的值替换字符串文字中的占位符.

您可以{}在插值字符串内的几对大括号()之间放置几乎任何表达式,该表达式将替换ToString为该表达式结果的表示形式.

当编译器遇到插值字符串时,它会立即将其转换为对String.Format函数的调用.正因为如此,您的第一个列表与写入基本相同:

throw new Exception(string.Format(
    "One or more errors occured during removal of the company:{0}{1}{2}", 
    Envrionment.NewLine, 
    Environment.NewLine, 
    exc.Message));
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,插值字符串允许您以更简洁的方式表达相同的内容,并且以更容易纠正的方式表达.