微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#6.0 – 意外字符’$’

我是编程新手,我刚刚安装了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> ..."

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

原文地址:https://www.jb51.cc/csharp/100235.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐