C
C#ā€¢2y ago
Jdance

ā” Primary constructors in VS 2019.

Probably going to be annoyed for not using 2022, yes I know. But moving on, I'm currently using .NET Standard 2.0 with the C# language version set to 'preview' in the .csproj. Is there any more steps I have to take so that I don't get 7 errors when trying to use primary constructors?
16 Replies
Trinitek
Trinitekā€¢2y ago
by default, standard 2.0 projects are set to an older c# language version
Jdance
JdanceOPā€¢2y ago
Ye, I'm aware, noticed the same issue with .NET Framework.
Trinitek
Trinitekā€¢2y ago
you need to use something like https://github.com/Sergio0694/PolySharp/, or bump up your framework target to net7 Framework 4.x targets will have the same limitations as Standard 2 šŸ¤” wait, primary ctors are a preview feature?
Jdance
JdanceOPā€¢2y ago
Yes.
Trinitek
Trinitekā€¢2y ago
you definitely need to be targeting the latest net8 preview in that case
Jdance
JdanceOPā€¢2y ago
Noticed when I was trying to work with console apps in VS Code. Sadly that doesn't work out in my case, I need to have my library be targeted for .NET Standard 2.0 Its intended use is on Mono, specifically for Unity 2020.3.
Wz
Wzā€¢2y ago
You need to use an sdk that has a new enough compiler which has the feature you want
Trinitek
Trinitekā€¢2y ago
I think I'd recommend you avoid preview lang features until that's officially added
Jdance
JdanceOPā€¢2y ago
Fair enough.
Trinitek
Trinitekā€¢2y ago
won't be too long now, release is in November
Jdance
JdanceOPā€¢2y ago
Just found the primary constructors pretty useful and wanted to simplify my code.
reflectronic
reflectronicā€¢2y ago
the only solution is to upgrade to Visual Studio 2022 that is the only version of Visual Studio that has a compiler which knows about primary constructors or anything released in C# 10 or 11 there is no hack you can do which will turn it on, the tool you are using is more than two years out of date by now
Jdance
JdanceOPā€¢2y ago
šŸ‘. I assume my VS 2019 solutions will transfer over fine?
Angius
Angiusā€¢2y ago
They're not dependent on the IDE
Jdance
JdanceOPā€¢2y ago
Just switched over to VS 2022. Thank you all for helping me figure this out. Always hesitated going through the effort of switching, but I'm starting to like the UI theme on VS 2022 a lot more, so it seems to be worth the switch.
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?