Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 210
slfan Avatar asked Sep 05 '25 16:09

slfan


2 Answers

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.

like image 103
slfan Avatar answered Sep 07 '25 17:09

slfan


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>
like image 23
Mohammad Mirmostafa Avatar answered Sep 07 '25 17:09

Mohammad Mirmostafa