Search code examples
c#.netvisual-studio.net-6.0visual-studio-2022

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)


Solution

  • 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