Re:C# 9 top-level programs and target-typed expressions
by WorldMaker on Tuesday March 30, 2021 @05:36PM
(#26640863)
Not by default, no. These two specific tricks should work on .NET Framework (some of the other tricks in C# after 7 or 8 require functionality that .NET Framework doesn't have, though). You can ask VS to use the more recent C# compiler with the LangVersion tag in the .csproj: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
Obviously, it's not suggested doing that nor is entirely well supported, but it is at least allowed.
Though if you are looking to use top-level programs you probably are working in greenfields anyway that should just be .NET 5+. It's not really a feature that makes a lot of sense for existing codebases as it is more a teaching tool/"quick and dirty command line app/script" tool.
Obviously, it's not suggested doing that nor is entirely well supported, but it is at least allowed.
Though if you are looking to use top-level programs you probably are working in greenfields anyway that should just be .NET 5+. It's not really a feature that makes a lot of sense for existing codebases as it is more a teaching tool/"quick and dirty command line app/script" tool.