【发布时间】: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 功能仍然受到限制。