【问题标题】:New match feature新匹配功能
【发布时间】:2016-09-01 19:33:04
【问题描述】:

我下载了 Enterprise 2015 Preview 3。如何让这个程序在 C#7 下运行?

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

abstract class Animal { }
class Dog : Animal
{
    public string BarkLikeCrazy()
    {
        return "WOOF WOOF WOOF";
    }
}

class Cat : Animal { }
class Swan : Animal { }

class Program
{
    static void Main(string[] args)
    {
        var animals = new Animal[] { new Dog(), new Cat(), new Swan() };

        var organizedAnimals = from animal in animals
                               let sound = animal match(
                                    case Dog d: "woof... " + d.BarkLikeCrazy()
                                    case Cat c: "meow"
                                    case * : "I'm mute.."
                              )
                              select new { Type = animal, Sound = sound };

        foreach (var animal in organizedAnimals)
        {
            Console.WriteLine($"{animal.Type.ToString()} - {animal.Sound}");
        }

        Console.ReadKey();
    }
}

【问题讨论】:

  • 你说的是 VS "15" Preview 3 吗?还是 VS 2015 更新 3?这很令人困惑,但这两者是完全分开的。另外,我认为match 不会出现在 C# 7.0 中。
  • 这是帮助显示的一部分:Microsoft Visual Studio Enterprise 15 Preview 3 Version 15.0.25428.1 PREVIEW Microsoft .NET Framework Version 4.6.01055 已安装版本:企业架构和建模工具 00369-50000-00000- AA450 Microsoft 架构和建模工具 Visual C# 15 Preview 3 00369-50000-00000-AA450 Microsoft Visual C# 15 Preview 3 Visual C++ 15 Preview 3 00369-50000-00000-AA450 Microsoft Visual C++ 15 Preview 3
  • 如果匹配不成功,那就太可惜了。它已经在 F# 中使用多年了。
  • 请参阅this article 了解 VS15 中的内容(顺便说一句,更新 4)。一些 C#7 功能仍然受到限制。

标签: c# c#-7.0


【解决方案1】:

将您的 match 关键字更改为 switch。

var organizedAnimals = from animal in animals
                       let sound = animal switch(
                            case Dog d: "woof... " + d.BarkLikeCrazy()
                            case Cat c: "meow"
                            case * : "I'm mute.."
                      )
                      select new { Type = animal, Sound = sound };

您可以在 discussion on GitHub 中阅读有关此内容的演变(在合并到模式匹配规范之前)。

这是来自GitHub feature discussion的一个例子:

var areas =
    from primitive in primitives
    let area = primitive switch (
        case Line l: 0,
        case Rectangle r: r.Width * r.Height,
        case Circle c: Math.PI * c.Radius * c.Radius,
        case *: throw new ApplicationException()
    )
    select new { Primitive = primitive, Area = area };

【讨论】:

  • 我得到了各种各样的错误,第一个是匹配“查询正文必须以 select 子句或 group 子句结尾”。我确实为项目定义了(下划线下划线)DEMO,DEMO_EXPERIMENTAL。
  • 我还注意到您链接到 Roslyn。不确定 Roslyn 与 2015 Enterprise Preview 3 的关系。
  • 您正在寻找新的 C#7 功能 - Roslyn 是 C# 编译器。听起来您需要完成这些步骤才能在项目中使用 C#7 功能。见:strathweb.com/2016/03/…
  • 看你上面的两个帖子。这是行不通的。我什至不知道下载 Roslyn 的链接是什么。我去了visualstudio.com/downloads/download-visual-studio-vs,但是这个VS2015社区也有同样的问题。
  • 我下载了包含VS实验版的VS2015 Enterprise。这个仍然不允许上面的代码既不匹配也不切换,但它确实暗示了 C# 语言的新特性正在慢慢滴入其中。
【解决方案2】:

如何让这个程序在 C#7 下运行?

你没有。基于表达式的模式匹配在 C# 7.0 的当前预览版中不可用,也不计划在 C# 7.0 的最终版本中使用。

The form that's currently planned for "C# 7.0 + 1" 看起来像这样:

var organizedAnimals = from animal in animals
                       let sound = animal switch (
                            case Dog d: "woof... " + d.BarkLikeCrazy(),
                            case Cat c: "meow",
                            case *: "I'm mute.."
                        )
                        select new { Type = animal, Sound = sound };

【讨论】:

  • 我刚刚更新到最新的 c# 预览版(17?),这仍然不起作用。
  • @Ivan VS 2017 RC 仍然只包含 C# 7.0。目前没有适用的 VS 或 C# 编译器版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 2018-01-07
相关资源
最近更新 更多