【问题标题】:Is C# 7 working in VS 15 Preview 4?C# 7 在 VS 15 Preview 4 中工作吗?
【发布时间】:2016-09-20 16:30:59
【问题描述】:

我尝试了一个简单的测试,但它不喜欢变量

作为一个简单的测试,我写了这个(也许它有一些简单的错误,但我也遇到了模式和元组的问题)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

    public class Program
    {
        static void Main(string[] args)
        {
            Runner runner = new ConsoleApplication2.Runner();
            Point p = new ConsoleApplication2.Point();
            runner.PrintCoordinates(p);
        }
    }


    public class Point
    {
        int x = 20;
        int y = 50;
        public void GetCoordinates(out int a, out int b)
        {
            a = x;
            b = y;
        }
    }

    public class Runner
    {
        public void PrintCoordinates(Point p)
        {
            p.GetCoordinates(out int x, out int y);
            Console.WriteLine($"({x}, {y})");       // x does not exist in current context
        }
    }
}

【问题讨论】:

    标签: c# c#-7.0


    【解决方案1】:

    根据this postPrintCoordinates示例方法来自哪里:

    注意:在预览版 4 中,范围规则更加严格:Out 变量的范围仅限于声明它们的语句。因此,上述示例在以后的版本之前将不起作用。

    新元组遇到了类似的问题,但您似乎可以通过 NuGet 下载部分解决该问题:

    注意:元组依赖于一组基础类型,这些类型未包含在 Preview 4 中。要使该功能正常工作,您可以通过 NuGet 轻松获取它们:

    • 右键单击解决方案资源管理器中的项目并选择“管理 NuGet 包...”
    • 选择“浏览”选项卡,选中“包含预发行版”并选择“nuget.org”作为“包源”
    • 搜索“System.ValueTuple”并安装它。

    【讨论】:

    • 是的,我也在Try Roslyn 中发现了这一点。目前我认为重点是if 声明和类似声明。
    • 我假设您可以克隆主存储库并自己构建它,因为当前的头似乎确实支持这种语法。
    • @DavidG:确实。对于更保守的人,总是在等待它:)
    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多