slfan
slfan

Reputation: 9129

How to activate C# 10 features Visual Studio 2022

I try to use the newest C# 10 features in Visual Studio 2022 Preview 3. The compiler does not recognize the new keywords required or field. global using seems to work.

public required string Name { get; init; }
public DateTime HiredDate{ get; init => field = value.Date(); }

Null parameter checking doesn't compile:

public void NullParameterCheck(string arg!!) { ... }

I also tried to set the language version to preview in the .csproj:

<LangVersion>preview</LangVersion>

Is there any setting I missed?

Upvotes: 4

Views: 5807

Answers (2)

Mohammad Mirmostafa
Mohammad Mirmostafa

Reputation: 1775

this is what I use in .csproj file:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>preview</LangVersion>
    <EnablePreviewFeatures>true</EnablePreviewFeatures>
    <GenerateRequiresPreviewFeaturesAttribute>true</GenerateRequiresPreviewFeaturesAttribute>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Upvotes: 1

slfan
slfan

Reputation: 9129

Finally I found part of the solution. I have to add

<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>

to the .csproj file. Null parameter checks work, but not required and field.

Upvotes: 7

Related Questions