Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling .NET 6 features per default

When creating a new project in VS 2022 with .NET 6, following new features are added automatically:

  • Top-Level Statements
  • Implicit Usings and Nullable References

Is there a way to create a .NET 6 project

  • without top-level-statements (a workaround other than creating a .NET 5 project & changing project type to .NET 6)

  • with implicit usings & nullable references disabled (without editing the project file each time after project creation)

like image 319
A.B. Avatar asked Feb 12 '26 22:02

A.B.


2 Answers

A. Nullable reference types - keep them on

I suggest keeping nullable reference types on. I'd echo Embracing nullable reference types and say that all new code should use it. Here's the original statement:

When .NET 5 rolls around, if we feel the nullable rollout phase has been a success, I could see us turning the feature on by default for new projects in Visual Studio. If the ecosystem is ready for it, there is no reason why any new code should ignore the improved safety and reliability you get from nullability annotations!

B. Top level statements

Update May 2022

VS2022 17.3.0 (Preview 1.1) supports disabling top-level statements when creating console apps: enter image description here


Original

Targetting the .net5 framework and editing the project file is the official way to use the old program style.

Please see Use the old program style.

While a .NET 6 console app template generates the new style of top-level statements programs, using .NET 5 doesn't. By creating a .NET 5 project, you'll receive the old program style. Then, you can edit the project file to target .NET 6 but retain the old program style for the Program.cs file.

and

The features that make the new program simpler are top-level statements, global using directives, and implicit using directives.

BTW. Cast your vote on the official Should the console project template use top-level statements directly in gihub:

enter image description here

like image 108
tymtam Avatar answered Feb 14 '26 16:02

tymtam


I came to this post to see if it would still be possible to have the Main() method when creating projects via command prompt. Looks like there is! as of .NET 6.0 SDK one can add the --use-program-main command line option when creating new projects as follows:

dotnet new console --use-program-main

Source: Use the old program style

like image 20
timmi4sa Avatar answered Feb 14 '26 15:02

timmi4sa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!