Skip to content

[EPIC] .NET 6 C# project templates use latest language idioms #3359

@DamianEdwards

Description

@DamianEdwards

In .NET 6, the included C# project templates will use the latest language idioms.

The following language features will be used or enabled by default in the SDK-included project templates as applicable:

Issues

Area owners

Template area Owner
Neutral (Console/Class lib) @KathleenDollard
Unit testing @kendrahavens
ASP.NET Core/gRPC/Worker @DamianEdwards
Blazor @danroth27
WinForms @OliaG
WPF @OliaG
MAUI @mhutch

Implementation Considerations

  • All changes detailed here will only be applied when the project is created targeting .NET 6+ (the default)
  • Some templates have a --langVersion option and will need to condition the relevant changes based on the supplied value so that only compatible features are utilized in the new project
  • Making the changes opt-in is not being considered at this time as it defeats the intended purpose which is to have the default experience for new projects reflect the latest language idioms
  • ❓ Should all changes detailed here have corresponding template options to allow opting-out of them at project creation time?
    • ☑️ Decision on this is "No" for now. We can revisit based on feedback if necessary.

Top-level statements

Global using directives

  • Language feature implementation issue
  • Introduced in C# 10
  • This feature is driven by SDK changes and as such no actual global using statements should appear in the template
  • Existing using statements in project templates that conflict with new SDK-driven default global using directives will be removed
  • The following namespaces are being globally included by the SDKs:
    • Microsoft.NET.SDK:
      • System
      • System.Collections.Generic
      • System.IO
      • System.Linq
      • System.Net.Http
      • System.Threading
      • System.Threading.Tasks
    • Microsoft.NET.SDK.Web:
      • Everything included by Microsoft.NET.SDK, plus:
      • System.Net.Http.Json
      • Microsoft.AspNetCore.Builder
      • Microsoft.AspNetCore.Hosting
      • Microsoft.AspNetCore.Http
      • Microsoft.AspNetCore.Routing
      • Microsoft.Extensions.Configuration
      • Microsoft.Extensions.DependencyInjection
      • Microsoft.Extensions.Hosting
      • Microsoft.Extensions.Logging
    • Microsoft.NET.SDK.Worker
      • Everything included by Microsoft.NET.SDK, plus:
      • Microsoft.Extensions.Configuration
      • Microsoft.Extensions.DependencyInjection
      • Microsoft.Extensions.Hosting
      • Microsoft.Extensions.Logging

File-scoped namespaces

  • Language feature implementation issue
  • Introduced in C# 10
  • Any namespaces declared in existing project templates will be changed to use the file-scoped namespaces syntax
  • ℹ️ This feature is not yet available in main builds but is expected to land during preview.7

Target-typed new expressions

  • Language feature documentation
  • Introduced in C# 9
  • Any constructor invocation where the type name can be omitted to reduce its duplication
    • ✔️ private readonly static object s_syncObj = new object(); to private readonly static object s_syncObj = new();
    • var someStrings = new List<string>(); stays as it is now as the type name is not repeated

Nullable reference types

  • Language feature documentation
  • Introduced in C# 8
  • Existing project templates will enable the feature by default by having <Nullable>enable</Nullable> added to their .csproj files
    • ℹ️ Enabling nullable reference types via SDK defaults was considered but rejected due to concerns with the experience during migration of existing projects to .NET 6, in that existing projects changed to target the .NET 6 TFM as part of migration will immediately exhibit compiler warnings associated with the feature being enabled and not handled correctly by the existing project code
  • .cs, .cshtml, and .razor files in existing project templates will be updated to appropriately handle nullable reference types
  • Code generation tools (e.g. Scaffolding, EF Core Migrations, etc.) should also be updated to ensure they produce code that is aligned with the nullable configuration of the target project.

async Main

  • Language feature documentation
  • Introduced in C# 7.1
  • Project templates with an existing Program.cs containing a static void Main method declaration with a body that includes calls to Task-returning methods (or methods with a Task-returning alternative) will be updated to declare the method as async and the calls to Task-returning methods will be updated with the await keyword.

Metadata

Metadata

Assignees

Labels

EpicGroups multiple user stories. Can be grouped under a theme.area: template-contentThe issue is related to content of template packages managed in this repo (/template_feed)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions