You need to be using C#9 or higher and .NET Framework Core 5 or 6.
See my repository for examples which are done in VS2019, work also in VS2022.
The project file will need
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>9.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
See also C# language versioning and Explore ideas using top-level statements to build code as you learn