Skip to content

Add article on new template output #25092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/core/tutorials/top-level-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: "Template changes in .NET 6"
description: The console app template now uses top-level statements. Understand what's changed and how to use existing learning materials with the new program.
ms.date: 07/13/2021
---
# New templates generate top-level statements

Starting with the .NET 6 Preview 7 SDK, the console app template generates the following code:

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This URL doesn't go anywhere meaningful yet, is that intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It will go to this article once published. (This article will be live before preview 7 goes public)

```

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

```csharp
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.

## Use the new program style

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

[Top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) 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.

*Global usings* means the compiler automatically imports a set of [`using` directives](../../csharp/language-reference/keywords/using-directive.md) based on the project type. For console applications, the following directives are implicitly included in every source file in the application:

- `using System;`
- `using System.Collections.Generic;`
- `using System.Linq;`
- `using System.Net.Http;`
- `using System.Net.Http.Json;`

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

These two features 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 first 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](../../csharp/whats-new/tutorials/top-level-statements.md).
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ items:
href: ../standard/glossary.md
- name: Tutorials
items:
- name: .NET 6 template changes
href: ../core/tutorials/top-level-templates.md
- name: Use Visual Studio
items:
- name: Create a console app
Expand Down