3

I am looking at and following example code that has the following lines:

public static DispenseOptionSketchVolume DoSketchVolume = new();
public static SketchSettingsPageData SketchSettingsPageData = new();
public static BlockVolumeData BlockVolumeData = new();
public static SketchEntityVolumePage SketchEntityVolume = new();

In my own project I created a framework 4.8 class library but get the error:

Feature 'target typed object creation' is not available. Please use language version 9.0 or greater. new(); is the error.

If I add this to my own csproj:

<PropertyGroup>
    <LangVersion>9.0</LangVersion>
</PropertyGroup>

Visual Studio asks me to attach to a process: enter image description here

In the example project the framework is set to 4.8, the type is a class Library, there is a framework that automatically registers the dll and it runs. There is no <LangVersion> directive in any file csproj or otherwise.

How could the example project compile the = new(); statements?

3
  • Humor me. Usually when the run button says "Attach" it's because no solution is open in the Solution Explorer. Do you have a solution open?
    – gunr2171
    Jan 17, 2022 at 20:56
  • 3
    C# 9 is supported on .NET 5 and up. While the specific feature you're asking for is purely a compiler thing, there are too many things in the new C# version that require runtime support that it wouldn't surprise me if the .NET 5 requirement is a hard limit. Jan 17, 2022 at 20:56
  • Ref. sergiopedri.medium.com/… — an article that claims various feature are (or at least one time would?) work via a manual LangVersion change. (So is then this manual setting ignored / not applied?) Jan 17, 2022 at 23:12

1 Answer 1

0

I just set <LangVersion>9.0</LangVersion> on a large .NET Framework 4.8 solution and it works as expected. The target-typed new is available and no error messages are shown. I successfully tried both VS2019 and VS2022. So it seems this does work. Maybe you need to make sure you're building with at least the .NET 5.0 compiler. Make sure your global.json is requiring at least a 5.0 SDK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.