Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# version 8 Target-typed new-expressions error requires (), [], or {} after type

I'm following a tutorial to learn the new features of c# version 8 using .Net Core 3.1

I'm working with latest version of Visual Studio 2019. while playing with some exercises i fond errors in the next code

Subject: Target-typed new-expressions

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"

did i miss something? i also tried to add to the project

<LangVersion>8.0</LangVersion>

any ideas?

like image 441
David Alejandro García García Avatar asked Oct 26 '25 03:10

David Alejandro García García


1 Answers

The syntax you are using is not valid, despite what you may find in any online tutorials. Target-typed new was originally planned for C# 8.0 but didn't end up making the final cut.

It is now (2020-05-10) marked with a milestone of "9.0 Candidate", meaning it's possible it won't be part of the next iteration either. See this github proposal/issue.

That being said, you can try the feature out on SharpLab by selecting the "C# Next: Target-typed new (9 Mar 2020)" branch.

like image 97
pinkfloydx33 Avatar answered Oct 28 '25 18:10

pinkfloydx33



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!