From acf1c19f4b733177f5695a985d956510ec790444 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 12 Jul 2021 17:43:28 -0400 Subject: [PATCH 1/8] Add article on new template output Fixes #25085 Describe the updated console app template changes, and point to more details on those language features. --- docs/core/tutorials/top-level-templates.md | 52 ++++++++++++++++++++++ docs/fundamentals/toc.yml | 2 + 2 files changed, 54 insertions(+) create mode 100644 docs/core/tutorials/top-level-templates.md diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md new file mode 100644 index 0000000000000..991cc06487079 --- /dev/null +++ b/docs/core/tutorials/top-level-templates.md @@ -0,0 +1,52 @@ +--- +title: "Template changes in .NET 6" +description: The console app template now uses top level statements. Understand what's different from learning resources created before this change. +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 +``` + +The new output leverages two 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 depended on the project name. +{ + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} +``` + +## Understand the new program + +The two features that make the new program simpler are top level statements and global 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 are generated as local functions nested inside the generated `Main` method. + +Global usings means the compiler imports a set of namespaces based on the project type. For console applications, the following namespaces are imported: + +- `System` +- `System.Collections.Generic` +- `System.Linq` +- `System.Net.Http` +- `System.Net.Http.Json` + +These namespaces are imported for every source file in the application. Other application types will include additional namespaces that are common for that application type. + +Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs your application. + +You can learn more about top level statements in the tutorial exploration on [top level statements](../../csharp/fundamentals/whats-new/tutorials/top-level-statements.md). diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 0d2139a2a0b4e..ddab6bf17a827 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -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 From 11122cd97949ffcef325aa17f929f66d852efd7f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 12 Jul 2021 18:12:48 -0400 Subject: [PATCH 2/8] fix broken link --- docs/core/tutorials/top-level-templates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index 991cc06487079..a661343676bae 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -49,4 +49,4 @@ These namespaces are imported for every source file in the application. Other ap Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs your application. -You can learn more about top level statements in the tutorial exploration on [top level statements](../../csharp/fundamentals/whats-new/tutorials/top-level-statements.md). +You can learn more about top level statements in the tutorial exploration on [top level statements](../../csharp/whats-new/tutorials/top-level-statements.md). From 88f8dd0eee337d3ea68703c7180619ef02fc88aa Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 10:12:26 -0400 Subject: [PATCH 3/8] proofread and edit --- docs/core/tutorials/top-level-templates.md | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index a661343676bae..0ae1cc270e577 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -1,9 +1,9 @@ --- title: "Template changes in .NET 6" -description: The console app template now uses top level statements. Understand what's different from learning resources created before this change. +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 +# New templates generate top-level statements Starting with the .NET 6 Preview 7 SDK, the console app template generates the following code: @@ -12,14 +12,14 @@ Console.WriteLine("Hello, World!"); // See https://aka.ms/new-console-template for more information ``` -The new output leverages two recent C# features that simplify the code you need to write for a program. Traditionally, the console template generated the following code: +The new output uses two 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 depended on the project name. +namespace MyApp // Note: actual namespace depends on the project name. { public class Program { @@ -31,11 +31,16 @@ namespace MyApp // Note: actual namespace depended on the project name. } ``` -## Understand the new program +These two forms represent the same program. Both are valid with C# 10.0. When you use the newer version, you need to write the body of the `Main` method. You don't need to write the other program elements by hand. You have two options you can follow with existing tutorials: -The two features that make the new program simpler are top level statements and global usings. +- 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. -[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 are generated as local functions nested inside the generated `Main` method. +## Use the new program style + +The two features that make the new program simpler are top-level statements and global 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 generated as local functions nested inside the generated `Main` method. Global usings means the compiler imports a set of namespaces based on the project type. For console applications, the following namespaces are imported: @@ -45,8 +50,10 @@ Global usings means the compiler imports a set of namespaces based on the projec - `System.Net.Http` - `System.Net.Http.Json` -These namespaces are imported for every source file in the application. Other application types will include additional namespaces that are common for that application type. +These namespaces are imported for every source file in the application. Other application types include more namespaces for that application type. + +Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs 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. -Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs your application. +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). +You can learn more about top-level statements in the tutorial exploration on [top level statements](../../csharp/whats-new/tutorials/top-level-statements.md). From 863aa3d8d352df564a18f1eb032cbffe395cffe4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 10:15:22 -0400 Subject: [PATCH 4/8] respond to feedback --- docs/core/tutorials/top-level-templates.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index 0ae1cc270e577..26d9ec8bd9b8e 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -40,17 +40,17 @@ These two forms represent the same program. Both are valid with C# 10.0. When y The two features that make the new program simpler are top-level statements and global 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 generated as local functions nested inside the generated `Main` method. +[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 imports a set of namespaces based on the project type. For console applications, the following namespaces are imported: +*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: -- `System` -- `System.Collections.Generic` -- `System.Linq` -- `System.Net.Http` -- `System.Net.Http.Json` +- `using System;` +- `using System.Collections.Generic;` +- `using System.Linq;` +- `using System.Net.Http;` +- `using System.Net.Http.Json;` -These namespaces are imported for every source file in the application. Other application types include more namespaces for that application type. +Other application types include more namespaces that are common for those application types. Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs 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. From 2af8a41bbf43a620f946fea97ab139fcc77f27f7 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 10:16:42 -0400 Subject: [PATCH 5/8] respond to feedback. --- docs/core/tutorials/top-level-templates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index 26d9ec8bd9b8e..77932e6924b7e 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -52,7 +52,7 @@ The two features that make the new program simpler are top-level statements and Other application types include more namespaces that are common for those application types. -Combined, these two features simplify the code that makes up your application. You can concentrate on the code that runs 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. +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. From ea09e56ebeabcdb964eb96a630e455ff680de42b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 10:59:34 -0400 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Kathleen Dollard --- docs/core/tutorials/top-level-templates.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index 77932e6924b7e..b8e0da1db825b 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -12,7 +12,7 @@ Console.WriteLine("Hello, World!"); // See https://aka.ms/new-console-template for more information ``` -The new output uses two recent C# features that simplify the code you need to write for a program. Traditionally, the console template generated the following code: +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; @@ -31,14 +31,14 @@ namespace MyApp // Note: actual namespace depends on the project name. } ``` -These two forms represent the same program. Both are valid with C# 10.0. When you use the newer version, you need to write the body of the `Main` method. You don't need to write the other program elements by hand. You have two options you can follow with existing tutorials: +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 +- Use the new program style, just including the code that makes up your application - Convert the new program style to the older style, with a `Program` class and a `Main` method. ## Use the new program style -The two features that make the new program simpler are top-level statements and global usings. +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. From 763c196798556d3d142da9c2e9281b9dba002ec5 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 11:03:22 -0400 Subject: [PATCH 7/8] grammar --- docs/core/tutorials/top-level-templates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index 77932e6924b7e..7ea771c935702 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -33,7 +33,7 @@ namespace MyApp // Note: actual namespace depends on the project name. These two forms represent the same program. Both are valid with C# 10.0. When you use the newer version, you need to write the body of the `Main` method. You don't need to write the other program elements by hand. You have two options you can follow with existing tutorials: -- Use the new program style, adding new top-level statements as you add features +- 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 From 2a6d7c70d31de9d37ddadafabe095883afd23d6e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 13 Jul 2021 11:20:51 -0400 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Andy (Steve) De George <67293991+adegeo@users.noreply.github.com> --- docs/core/tutorials/top-level-templates.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/core/tutorials/top-level-templates.md b/docs/core/tutorials/top-level-templates.md index a8fbdd4e5165f..cf67d1a46541d 100644 --- a/docs/core/tutorials/top-level-templates.md +++ b/docs/core/tutorials/top-level-templates.md @@ -12,7 +12,7 @@ Console.WriteLine("Hello, World!"); // See https://aka.ms/new-console-template for more information ``` -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: +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; @@ -31,7 +31,7 @@ namespace MyApp // Note: actual namespace depends on the project name. } ``` -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: +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.