.NET 6 was released in November of 2021. It's the latest and greatest from Microsoft, bringing great new features, tooling, and quality-of-life improvements for developers. This blog post will shed some light on these new advancements to .NET and discuss the state of the ecosystem as a whole. If you want to learn more about the history of .NET, feel free to check out our other blog post on The History of .NET.
New Features
Top Level Statements
In .NET 6, you no longer need to write out the Program class and Main method that we all know and love. Top Level Statements is a feature that allows developers to start writing code at the root level of the Program.cs file without a namespace, Program class, or Main method declaration.
Top Level Statements make it so that this....
Can become this...
Top-level statements remove a lot of bloat from simple apps. Behind the scenes, .NET will still generate a Program class and Main method for you. It is also important to note that you are not required to use Top Level Statements in .NET 6. The code in the first picture would work fine.
Global and Implicit Using Statements
Global using statements enable you to specify namespaces accessible to the entire project. You can declare global using statements in any .cs file in your project. The syntax is incredibly simple; just add the keyword global before your using statement.
By default, .NET 6 projects will also include a set of implicit global usings based on the app model you choose. You can turn this off with the following tag in your .csproj file.
<ImplicitUsings>disable</ImplicitUsings>
To see the list of implicit usings for each project model, visit this link: https://docs.microsoft.com/en-us/dotnet/core/project-sdk/overview#implicit-using-directives
File Scoped Namespaces
.NET 6 introduces tons of quality-of-life improvements for developers. One of my favorites is File Scoped Namespaces. This allows you to specify the namespace of an entire .cs file rather than using a namespace block. As a result, all your code is indented just slightly less, and I really love that.
.NET Core 3.1 & .NET 5
NET 6
New LINQ Methods
In .NET 6 they released new LINQ methods as well. Some of the most notable are .Chunk(), .MaxBy(), and .MinBy(). They introduced many .*By() methods that allow you to perform LINQ operations contingent on a given expression like .IntersectBy().
More information can be found here: https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-6#new-linq-apis
DateOnly and TimeOnly
DateTime has been a part of .NET since the very first version of .NET Framework. The construct represents a single point in time using the combination of date and time information. With the release of .NET 6, developers now can store dates and times in separate constructs using DateOnly and TimeOnly.
More info: https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/
Program.cs and Startup.cs Consolidation
With the introduction of ASP.NET Core, came the generic hosting provider. For the last ~5 years, modern ASP.NET code was typically bootstrapped using the Program.cs and Startup.cs class. The Program.cs file configured the hosting environment while the Startup.cs configured dependency injection and middleware.
With .NET 6, developers no longer need to create a Startup.cs class. In fact, the default ASP.NET 6 template for Web API doesn't create a Startup.cs class at all.
Minimal APIs
Minimal APIs are a way to define small API code without using Controllers. It's a nice way to define simple endpoints without using the convention-based routing in MVC and Web API.
Visual Studio 2022
Visual Studio is the IDE of choice for many .NET developers. Its tight integration with .NET and C# makes it a .NET developer's best friend, but its heavyweight interface and clunky performance at times can make it a developer's worst enemy. Along with the launch of .NET 6, came the latest version of Visual Studio since 2019: Visual Studio 2022. It aims at improving developer productivity in a few ways that we'll outline next.
x64
Visual Studio 2022 is the first-ever version to be 64-bit. Before this shift, VS was limited to ~4GB of memory in the main process. One of the advantages of more memory access is the ability to quickly load more projects in a solution. This also will reduce the amount of out-of-memory exceptions VS encounters.
More info: https://devblogs.microsoft.com/visualstudio/visual-studio-2022/
Hot Reload (and drama)
Hot Reload is one of the most talked-about updates to VS 2022. It allows for changes to files in VS without recompiling (or even refreshing in some cases) to a variety of different app models. An example of hot reload in action would be updating XAML in .NET MAUI. The mobile emulator can receive the updated UI changes without recompiling. It sounds like the Visual Studio team has big aspirations for Hot Reload in the future, so this may just be the beginning.
Hot Reload was also the source of a lot of drama surrounding the viability of .NET as an open-source project. A Microsoft executive made the decision that Hot Reload would be a premium feature exclusive to Visual Studio. This decision was made after thousands of lines of open-source code was already written to be included in the .NET CLI for dotnet watch. The community took great issue with this decision. Many big names in the .NET community spoke out on social media and wrote blog posts. Eventually Microsoft listened and Hot Reload made its way back into the .NET CLI.
Many developers have had skepticism about Microsoft and Open-Source Software and this incident didn't do much to help their public image.
More info: https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/
https://dusted.codes/can-we-trust-microsoft-with-open-source
https://devblogs.microsoft.com/dotnet/net-hot-reload-support-via-cli/
IntelliSense and Intellicode
One of the first things I noticed when using VS 2022 preview last year was the much more aggressive IntelliSense and Intellicode. They didn't discuss this too much in the official blog posts, but it's something you'll notice when making the switch from VS 2019. IntelliSense provides you static type analysis and inference, whereas Intellicode uses AI to learn from the code you write and predict what you may write next.
Here is an example where Intellicode predicts what I want in a foreach statement. It honestly does a pretty good job.
.NET Multi-Platform App UI (MAUI)
Next Evolution of Xamarin.Forms
Since ~2016, Xamarin.Forms has been the app model for building mobile applications on .NET. The release of .NET 6 also gives us a successor to Xamarin.Forms. .NET MAUI is the next evolution of Xamarin.Forms and has been in preview for over a year. It was intended to launch with the release of .NET 6, but has been running a bit late. .NET MAUI has been in preview since the launch of .NET 6. The MAUI team just announced the availability of a release candidate on April 12th of 2022. .NET MAUI will allow developers to target iOS, Android, WinUI, and macOS.
Single Project
One of the biggest features of .NET MAUI is the single project format. This allows developers to target different environments all from the same project in Visual Studio. This should be a nice improvement compared to Xamarin.Forms, in which each target environment was a separate project.
What's Next?
.NET 7 is on the way!
The .NET train keeps on moving! .NET 7 is already in public preview, you can go try it out right now.
The Future of .NET Versioning
November is for .NET! Every November you can expect a new, major version of .NET to be released. The plan is for each even-numbered release to be a Long-Term Support (LTS) version. LTS means the version will receive software patches for three years after the initial release whereas the non-LTS versions will only receive patches for half that time.
Source: https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core
If you are interested in learning more about .NET Versioning and its history, check out our post on the history of .NET Versioning here -> https://omnitech-inc.com/blog/the-history-of-net/
Conclusion
.NET 6 has arrived, and .NET 7 is on the way! It is an exciting time to be a .NET developer. New features and tools continue to be developed so we must keep learning. We recommend upgrading as soon as it makes sense, so get started using .NET 6 today and let us know if you need any help. Here at Omnitech we love .NET, and we are pretty good at it too. Happy Coding!