Skip to content

Commit 8fafeef

Browse files
BillWagnerKathleenDollardadegeo
authored
Add article on new template output (#25092)
* Add article on new template output Fixes #25085 Describe the updated console app template changes, and point to more details on those language features. * fix broken link * proofread and edit * respond to feedback * respond to feedback. * Apply suggestions from code review Co-authored-by: Kathleen Dollard <[email protected]> * grammar * Apply suggestions from code review Co-authored-by: Andy (Steve) De George <[email protected]> Co-authored-by: Kathleen Dollard <[email protected]> Co-authored-by: Andy (Steve) De George <[email protected]>
1 parent 33c8aa3 commit 8fafeef

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: "Template changes in .NET 6"
3+
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.
4+
ms.date: 07/13/2021
5+
---
6+
# New templates generate top-level statements
7+
8+
Starting with the .NET 6 Preview 7 SDK, the console app template generates the following code:
9+
10+
```csharp
11+
Console.WriteLine("Hello, World!");
12+
// See https://aka.ms/new-console-template for more information
13+
```
14+
15+
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:
16+
17+
```csharp
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
22+
namespace MyApp // Note: actual namespace depends on the project name.
23+
{
24+
public class Program
25+
{
26+
public static void Main(string[] args)
27+
{
28+
Console.WriteLine("Hello World!");
29+
}
30+
}
31+
}
32+
```
33+
34+
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:
35+
36+
- Use the new program style, adding new top-level statements as you add features.
37+
- Convert the new program style to the older style, with a `Program` class and a `Main` method.
38+
39+
## Use the new program style
40+
41+
The features that make the new program simpler are top-level statements, global usings, and implicit usings.
42+
43+
[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.
44+
45+
*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:
46+
47+
- `using System;`
48+
- `using System.Collections.Generic;`
49+
- `using System.Linq;`
50+
- `using System.Net.Http;`
51+
- `using System.Net.Http.Json;`
52+
53+
Other application types include more namespaces that are common for those application types.
54+
55+
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.
56+
57+
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.
58+
59+
You can learn more about top-level statements in the tutorial exploration on [top level statements](../../csharp/whats-new/tutorials/top-level-statements.md).

docs/fundamentals/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ items:
7070
href: ../standard/glossary.md
7171
- name: Tutorials
7272
items:
73+
- name: .NET 6 template changes
74+
href: ../core/tutorials/top-level-templates.md
7375
- name: Use Visual Studio
7476
items:
7577
- name: Create a console app

0 commit comments

Comments
 (0)