-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
acf1c19
Add article on new template output
BillWagner 11122cd
fix broken link
BillWagner 88f8dd0
proofread and edit
BillWagner 863aa3d
respond to feedback
BillWagner 2af8a41
respond to feedback.
BillWagner ea09e56
Apply suggestions from code review
BillWagner 763c196
grammar
BillWagner 6615861
Merge branch 'app-template-updates' of https://github.com/BillWagner/…
BillWagner 2a6d7c7
Apply suggestions from code review
BillWagner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
||
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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)