【问题标题】:C# 6.0 - Unexpected character '$' [closed]C# 6.0 - 意外字符'$' [关闭]
【发布时间】:2017-09-27 09:20:08
【问题描述】:

我是编程新手,我刚刚安装了 Visual Studio 2017。我创建了这段代码(来自我正在学习的书),但这无法编译。我有字符串插值问题,我得到错误:

意外字符'$',

但我使用的是 C# 6.0,所以这应该不是问题吗?

static void Main(string[] args)   
{   
     string comparison;   
     WriteLine("Enter the number:");   
     double var1 = ToDouble(ReadLine());   
     WriteLine("Enter another number :");   
     double var2 = ToDouble(ReadLine());   
     if (var1 < var2)   
         comparison = "less than";   
     else   
     {   
         if (var1 == var2)   
             comparison = "equal to";   
         else   
             comparison = "greater than";    
      }   

     WriteLine($ "The first number is {comparison} the second number");
     ReadKey();   
}

【问题讨论】:

  • 尽管这只是一个间距问题,但我不同意它因此而被搁置。这导致了一个编译错误,起初看起来很奇怪,因为间距在 C# 的许多部分都不是问题

标签: c# .net c#-6.0 string-interpolation


【解决方案1】:

这是一个很小的问题:) $后面的空格去掉:

WriteLine($"The first number is {comparison} the second number");

查看documentation下的正确结构:

$"<text> {<interpolated-expression> [,<field-width>] [:<format-string>] } <text> ..."

我已请求编辑说明$ 之后不得有空格,现在它声明:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多