2017-09-27 91 views
7

我是编程新手,刚刚安装了Visual Studio 2017.我创建了这个代码(从我正在学习的书中),但是这不能编译。我有串插的问题,我得到错误:

Unexpected character '$',

但我使用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(); 
} 
+1

即使这只是一个间距问题,我不同意这个问题,因为这个原因暂时搁置。这导致了一个编译错误,起初看起来很奇怪,因为在C#的许多部分中间距不是问题。 –

回答

11

这是一个非常小的问题:) $后删除空间:

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

documentation下,结构合理:

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

我要求解释的编辑有在$之后必须没有间距,现在它指出:

enter image description here

相关问题