使用 VS 2015 打开 VS 2017 项目时出现语法错误 [重复]

Posted

技术标签:

【中文标题】使用 VS 2015 打开 VS 2017 项目时出现语法错误 [重复]【英文标题】:syntax errors when opening VS 2017 project with VS 2015 [duplicate] 【发布时间】:2019-05-21 00:58:31 【问题描述】:

我有一个 Visual Studio 2017 项目,想用 Visual Studio 2015 打开它。

在我的 C# 代码中,我使用这种方法

    public static bool TryGetValue(this CustomProperties props, string key, out string value)
    
        try
        
            CustomProperty prop = props[key];
            value = prop.Value;
            return true;
        
        catch (Exception e)
        
            Console.WriteLine(e);
            value = string.Empty;
            return false;
        
    

从集合中获取值。在构建项目时,我得到了无效的表达术语。

当我使用这行代码时

        if (props.TryGetValue("username", out string username)) // string is an invalid expression
        
            edtUsername.Text = username; // this is unknown because the expression above throws errors
        

Visual Studio 2015 说“字符串”是一个无效的表达式。我该如何解决这个问题?

【问题讨论】:

你的意思是这段代码在VS2017中编译成功? 不要使用 Visual Studio 2015。out string username 是 C# 7 功能。你不能在 VS 2015 中使用 C# 7 【参考方案1】:

out 运算符之外声明字符串变量

string username;
if (props.TryGetValue("username", out username)) 

    edtUsername.Text = username; 

【讨论】:

这恢复到 C# 6,它不会使 VS 2015 与 C# 7 一起工作

以上是关于使用 VS 2015 打开 VS 2017 项目时出现语法错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章