为什么不能在字符串前使用@ $前缀?

时间:2019-01-31 10:16:07

标签: c# string prefix string-interpolation

所有这些字符串前缀在C#中合法使用:

  "text"
 @"text"
 $"text"
$@"text"

这不是为什么吗?

@$"text"

人们会认为这些运算符的顺序无关紧要,因为它们在C#中除了前缀字符串之外没有其他含义。我想不出这种反向双前缀无法编译的情况。该命令是否仅出于美学目的执行?

3 个答案:

答案 0 :(得分:8)

这些前缀不是运算符。它们仅由编译器解释。尽管它了解$@,但不了解@$。为什么?因为微软的编译器团队决定这样做。

但是,对后者的支持是planned for C# 8.0

答案 1 :(得分:7)

不允许插入内插逐字字符串,除其他原因外,没有其他原因。

好消息是,在不久的将来即将发布的C#版本8中,将允许这样做。如果安装Visual Studio 2019预览版,则可以尝试一下。

答案 2 :(得分:1)

根据msDocs

  

逐字插入的字符串以const mongoose = require('mongoose'); const Schema = mongoose.Schema; const AdminSchema = new Schema({ name: { type: String, required: true }, email: { type: String, required: true, maxlength: 1 }, skype_id: { type: String }, password: { type: String, required: true } }); 字符开头,后跟   $字符。

     

在插入的逐字记录中,$令牌必须出现在@令牌之前   字符串。

也许这是它们被设计为当前版本的C#编译器可以理解的方式。

相关问题