C# 6.0 - Unexpected character '$' [closed]

别等时光非礼了梦想. 提交于 2020-07-03 06:13:43

问题


I'm new to programming and I have just installed Visual Studio 2017. I created this code (from the book I'm learning), but this does not compile. I have problem with string interpolation and I get error:

Unexpected character '$',

but I'm using C# 6.0 so this should not be a problem ?

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:


It is a very small problem :) Remove space after $:

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

See proper structure under documentation:

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

I've requested an edit that explains that there must be no spacing after the $ and now it states:



来源:https://stackoverflow.com/questions/46444054/c-sharp-6-0-unexpected-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!