新的 C# 6.0 字符串插值语句无法编译 [英] New C# 6.0 String Interpolation statements don't compile

查看:22
本文介绍了新的 C# 6.0 字符串插值语句无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现此视频中的一些功能,但是我对新的 String Interpolation 语法不太走运(我有其他一切工作,至少在这段代码中).

我使用的是 Visual Studio 2015 CTP6,我已将其配置为使用 .NET 4.6 并进入构建选项以确保我指定的是 C# 6.0.我也遵循此处的说明.>

这是我的代码:

使用系统;使用静态 System.Math;命名空间 NewCsharp6Features{公共类 C6Point{公共 int X { 得到;}公共 int Y { 得到;}公共双距离 =>平方(X * X + Y * Y);公共 C6Point(int x, int y) { X = x;y = y;}公共覆盖字符串 ToS​​tring(){返回 "(\{X}, \{Y})";}}}

我收到两个编译错误:

<块引用>

CS1009 |无法识别的转义序列

知道我做错了什么吗?

解决方案

你需要用 $

处理字符串

 公共覆盖字符串 ToS​​tring(){返回 $"({X}, {Y})";}

I am trying to implement some of the features in this video but I'm not having much luck with the new String Interpolation syntax (I have everything else working, at least that's in this code).

I am using Visual Studio 2015 CTP6, I have configured it to use .NET 4.6 and have gone into the Build options to make sure I was specifying C# 6.0. I have also followed the instructions here.

Here is my code:

using System;
using static System.Math;

namespace NewCsharp6Features
{
    public class C6Point
    {
        public int X { get; }
        public int Y { get; }
        public double Distance => Sqrt(X * X + Y * Y);

        public C6Point(int x, int y) { X = x; Y = y; }

        public override string ToString()
        {
            return "(\{X}, \{Y})";
        }
    }
}

I am getting two of this compilation error:

CS1009 | Unrecognized escape sequence

Any idea what I'm doing wrong, here?

解决方案

You need to proceed the string with a $

        public override string ToString()
        {
            return $"({X}, {Y})";
        }

这篇关于新的 C# 6.0 字符串插值语句无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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