.NET 6 marks the completion of Microsoft’s plan to unify the different .NET implementations. The unification of .NET is not simply merging code from different sources into one API. There are quite some enhancements and new features that were added to the runtime and language.
- Improved Performance
- Hot Reload
- System.Linq enhancements
- Minimal Web API
- New features in C# 10
The goal for .NET unification is to simplify the choices and learning curve for new developers, and at the same time make it easier for experienced developers. That’s why the minimal APIs for hosting and routing in web applications, and new project templates were introduced.
Simplified Start-Up Code and Minimal API
The .NET SDK project templates were updated to use modern C# language features and patterns such as:
- Top-level statements
- async Main
- Global and implicit using directives
- File-scoped namespaces
- Target-typed new expressions
- Nullable reference types
What does this mean? When you run the command dotnet new console to create a new Console project, this is what you get.
csharp Console.WriteLine("Hello, World!");
You can add more code to Program.cs, just like you can add more statements to the Main method in the old template. You can even add functions, which will be created as local functions nested inside the generated Main method. This streamlines the experience to make it easier to onboard new developers, and make it faster for experienced developers to start building without unnecessary code like before.
Introducing minimal APIs
The experience is also simple for new web developers and existing customers who want to build microservices or small HTTP APIs. The new web template gives you a fully functional HTTP API just using a single file with a few lines of code. When you run dotnet new web, you get a project with a single endpoint defined in Program.cs.
csharp var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run();
Concerns about the new Templates
Not everyone wants these new project templates, and the reasons vary from person to person. Some have asked to have both the new and old template in the SDK, while some would like to have these new features available as options when creating the project. All for the flexibility to opt-out of the new feature template and continue with the familiar, traditional template. The team at Microsoft has made it clear that they do not plan on changing the template for .NET 6 GA.
If you want to use the old style with an explicit Main function and using statements, you can add them manually and disable implicit using in the project settings.
There’s also an unofficial template that can be used to create projects with the old style. It’s from Jonathan Pobst and you can install it using the command dotnet new -i Classic.Console.Templates. To create a new project from the command line, you run dotnet new console-classic. The options available for the CLI are:
- --nrt: If true, turns on nullable reference types. [defaults to 'true'] - --topLevelProgram: If true, auto-generates the top-level statements, skipping explicit main methods, class and namespace. [defaults to 'false'] - --implicitUsings: If true, auto-imports many common using statements. [defaults to 'false']
You can also use the Classic.Console.Templates template from Visual Studio and select your preferred options.
Uno Platform Templates
Uno Platform will support .NET 6 upon its launch. However, the change in minimal API templates does not affect Uno Platform at all as it comes with its own templates https://platform.uno/docs/articles/get-started-dotnet-new.html. If you want to learn more about Uno Platform’s 2022 roadmap, join us November 30th at UnoConf.
.NET 6 is a big step towards having a unified .NET platform. There are many wonderful features like hot reloading, minimal API, Inner-Loop performance, and many more. The new features and patterns provide a streamlined experience to make it easier and faster to build applications. Join us at .NET Conf 2021 where .NET 6 will be launched, and learn more about the new capabilities in the .NET Platform.
Thank you Peter Mbanugo for helping curate a great article on this contentious topic.