如何在现有项目中启用 C# 7 功能 [英] How to enable C# 7 features in existing projects

查看:27
本文介绍了如何在现有项目中启用 C# 7 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何在现有解决方案中使用新的 C# 7 功能.我尝试在 switch 语句中使用模式匹配,但我一直收到 Value of integer type expected 错误.

I am not sure how to use new C# 7 features in existing solution. I tried using pattern matching in a switch statement but I keep getting Value of integral type expected error.

是否有启用它的技巧?如果我在 VS 2017 中打开解决方案,我就可以使用新功能.

Is there a trick to enable it? I though I can just use new features if I open the solution in VS 2017.

我的项目面向 .net 4.6.2.

My projects are targeting .net 4.6.2.

这是示例代码

    private void CS7Test(object o)
    {
        switch (o)
        {
            case null:
                Console.WriteLine("it's a constant pattern");
                break;
            case int i:
                Console.WriteLine("it's an int");
                break;
            case UserInfo p when p.Username.StartsWith("Ka"):
                Console.WriteLine($"a Ka person {p.Username}");
                break;
            case UserInfo p:
                Console.WriteLine($"any other person {p.Username}");
                break;
            case var x:
                Console.WriteLine($"it's a var pattern with the type {x?.GetType().Name} ");
                break;
            default:
                break;
        }
    }

推荐答案

实际上它不需要做任何特殊的配置就可以工作.Resharper 出现了这些错误,在禁用 resharper 后,它的工作非常出色.

Actually it's working without doing any special config. Resharper was giving those errors and after disabling the resharper it worked like a charm.

这篇关于如何在现有项目中启用 C# 7 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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