C#6.0-意外字符"$" [英] C# 6.0 - Unexpected character '$'

查看:32
本文介绍了C#6.0-意外字符"$"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,我刚刚安装了Visual Studio2017.我创建了此代码(从我正在学习的书中),但是无法编译.字符串插值有问题,但出现错误:

意外字符'$',

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

 静态void Main(string [] args){字符串比较;WriteLine(输入数字:");double var1 = ToDouble(ReadLine());WriteLine(输入另一个数字:");double var2 = ToDouble(ReadLine());如果(var1< var2)比较=小于";别的{如果(var1 == var2)比较=等于";别的比较=大于";}WriteLine($第一个数字是{comparison}第二个数字");ReadKey();} 

解决方案

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

  WriteLine($第一个数字是{比较}第二个数字"); 

请参见

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();   
}

解决方案

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:

这篇关于C#6.0-意外字符"$"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆