C
C#ā€¢12mo 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ā€¢12mo ago
by default, standard 2.0 projects are set to an older c# language version
Jdance
Jdanceā€¢12mo ago
Ye, I'm aware, noticed the same issue with .NET Framework.
Trinitek
Trinitekā€¢12mo 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
Jdanceā€¢12mo ago
Yes.
Trinitek
Trinitekā€¢12mo ago
you definitely need to be targeting the latest net8 preview in that case
Jdance
Jdanceā€¢12mo 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ā€¢12mo ago
You need to use an sdk that has a new enough compiler which has the feature you want
Trinitek
Trinitekā€¢12mo ago
I think I'd recommend you avoid preview lang features until that's officially added
Jdance
Jdanceā€¢12mo ago
Fair enough.
Trinitek
Trinitekā€¢12mo ago
won't be too long now, release is in November
Jdance
Jdanceā€¢12mo ago
Just found the primary constructors pretty useful and wanted to simplify my code.
reflectronic
reflectronicā€¢12mo 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
Jdanceā€¢12mo ago
šŸ‘. I assume my VS 2019 solutions will transfer over fine?
Angius
Angiusā€¢12mo ago
They're not dependent on the IDE
Jdance
Jdanceā€¢12mo 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ā€¢12mo 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.
Want results from more Discord servers?
Add your server
More Posts