加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#6.0 – 意外字符’$’

发布时间:2020-12-15 07:59:40 所属栏目:百科 来源:网络整理
导读:我是编程新手,我刚刚安装了Visual Studio 2017.我创建了这段代码(来自我正在学习的书),但这不能编译.我有字符串插值的问题,我得到错误: Unexpected character ‘$’, 但我正在使用C#6.0,所以这应该不是问题? static void Main(string[] args) { string com
我是编程新手,我刚刚安装了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();   
}

解决方法

这是一个非常小的问题:)删除$后的空格:
WriteLine($"The first number is {comparison} the second number");

请参阅documentation下的正确结构:

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

我已经请求了一个编辑,解释了在$之后必须没有间距,现在它说明:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读