【问题标题】:c# version 8 Target-typed new-expressions error requires (), [], or {} after typec# version 8 Target-typed new-expressions error requires (), [], or {} after type
【发布时间】:2020-05-09 23:23:03
【问题描述】:

我正在学习使用 .Net Core 3.1 学习 c# 版本 8 的新功能的教程

我正在使用最新版本的 Visual Studio 2019。 在玩一些练习时,我喜欢在下一个代码中出现错误

主题:目标类型的新表达式

Dictionary<string, List<int>> field = new Dictionary<string, List<int>>();
Dictionary<string, List<int>> field = new(); //this throws the error "requires (), [], or {} after type requires (), [], or {} after type"

我错过了什么吗? 我也尝试添加到项目中

<LangVersion>8.0</LangVersion>

有什么想法吗?

【问题讨论】:

标签: c# .net-core .net-core-3.1 c#-8.0


【解决方案1】:

您使用的语法无效,尽管您可以在任何在线教程中找到内容。目标类型的new 最初计划用于 C# 8.0,但最终没有做出最终决定。

现在 (2020-05-10) 标有“9.0 候选”里程碑,这意味着它可能不会成为下一次迭代的一部分任何一个。见this github proposal/issue

话虽如此,您可以通过选择“C# Next: Target-typed new (9 Mar 2020)”分支在 SharpLab 上试用该功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2019-01-14
    • 2021-08-06
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多