New C# templates generate top-level statements

Starting with .NET 6, new projects using the console template generate different code than previous versions:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

The new output uses recent C# features that simplify the code you need to write for a program. Traditionally, the console app template generated the following code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

These two forms represent the same program. Both are valid with C# 10.0. When you use the newer version, you only need to write the body of the Main method. You don't need to include the other program elements. You have two options to work with existing tutorials:

  • Use the new program style, adding new top-level statements as you add features.
  • Convert the new program style to the older style, with a Program class and a Main method.

If you want to use the old templates, see the Use the old program style section.

Use the new program style

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

Top-level statements means the compiler generates the namespace, class, and method elements for your main program. You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can even add functions. They're created as local functions nested inside the generated Main method.

Both top-level statements and implicit using directives simplify the code that makes up your application. To follow an existing tutorial, add any new statements to the Program.cs file generated by the template. You can imagine that the statements you write are between the open and closing braces in the Main method in the instructions of the tutorial.

If you'd prefer to use the older format, you can copy the code from the second example in this article, and continue the tutorial as before.

You can learn more about top-level statements in the tutorial exploration on top-level statements.

Implicit using directives

Implicit using directives mean the compiler automatically adds a set of using directives based on the project type. For console applications, the following directives are implicitly included in the application:

  • using System;
  • using System.IO;
  • using System.Collections.Generic;
  • using System.Linq;
  • using System.Net.Http;
  • using System.Threading;
  • using System.Threading.Tasks;

Other application types include more namespaces that are common for those application types.

If you need using directives that aren't implicitly included, you can add them to the .cs file that contains top-level statements or to other .cs files. For using directives that you need in all of the .cs files in an application, use global using directives.

Disable implicit using directives

If you want to remove this behavior and manually control all namespaces in your project, add <ImplicitUsings>disable</ImplicitUsings> in the project file.

Global using directives

A global using directive imports a namespace for your whole application instead of a single file. These global directives can be added either by adding a <Using> item to the project file, or by adding the global using directive to a code file.

You can also add a <Using> item in your project file to remove a specific implicit using directive. For example, if the implicit usings feature is turned on with <ImplicitUsings>enable</ImplicitUsings>, adding the following <Using> item removes the System.Net.Http namespace from those that are implicitly imported:

<ItemGroup>
  <Using Remove="System.Net.Http" />
</ItemGroup>

Use the old program style

While a .NET 6 console app template will generate 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.

Important

Creating a project that targets .NET 5 requires the .NET 5 templates. The .NET 5 templates can be installed manually with the dotnet new --install command or by installing the .NET 5 SDK.

  1. Create a new project

    dotnet new console --framework net5.0
    
  2. Open the project file in a text editor and change <TargetFramework>net5.0</TargetFramework> to <TargetFramework>net6.0</TargetFramework>.

    Here's a file diff that illustrates the changes:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
    -   <TargetFramework>net5.0</TargetFramework>
    +   <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  3. Optional step: you can still use some of the newer .NET 6 and C# features by adding the properties for implicit using directives and nullable context to the project file.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
    +   <ImplicitUsings>enable</ImplicitUsings>
    +   <Nullable>enable</Nullable>
      </PropertyGroup>
    
    </Project>
    

Use the old program style in Visual Studio

When you create a new console project in Visual Studio, you're prompted with a dropdown box that identifies which target framework you want to use. Change that value to 5.0. After the project is created, edit the project file to change it back to 6.0.

  1. When you create a new project, the setup steps will navigate to the Additional information setup page. On this page, change the framework setting from .NET 6.0 (Long-term support) to .NET 5.0, and then select the Create button.

    Visual Studio select the target .NET Framework 5.0

  2. After your project is created, find the Project Explorer pane. Double-click on the project file and change <TargetFramework>net5.0</TargetFramework> to <TargetFramework>net6.0</TargetFramework>.

    Here's a file diff that illustrates the changes:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
    -   <TargetFramework>net5.0</TargetFramework>
    +   <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    

    Alternatively, you can right-click on the project in the Project Explorer pane, and select Properties. This opens up a settings page where you can change the Target framework.

    Edit the project properties for Visual Studio and set the .NET Framework version.

  3. Optional step: you can still use some of the newer .NET 6 and C# features by adding the properties for implicit using directives and nullable context to the project file.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
    +   <ImplicitUsings>enable</ImplicitUsings>
    +   <Nullable>enable</Nullable>
      </PropertyGroup>
    
    </Project>
    

Template feedback

Top-level statements is a new feature in .NET 6. Add an up or down vote in GitHub issue #26313 to let us know if you support the use of this feature in project templates.