.NET 8 is on the way! +10 Features that will blow your mind 🤯
Tags: csharp, dotnet, news, programming
Published: 2023-05-18 10:50:44

In this article, we’re going to delve into the latest updates and enhancements introduced in .NET 8 Preview 4. Microsoft has been hard at work, refining and expanding the capabilities of the .NET ecosystem, and this preview release is no exception.

We’ll be exploring the key features, such as improvements to System.Text.Json, code generation optimizations, and support for unspeakable types.

Let’s check what’s new in .NET 8 Preview 4!

## A Fresh Take on MSBuild Output

Ever struggled with MSBuild output? Parsing that wall of text can be a nightmare, right? Well, Microsoft heard you and now we’ve got the Terminal Logger! This modernized MSBuild output logging is here to make your life easier. It’s designed to:

-   Organize errors logically by project
-   Display projects and builds in a user-friendly way (especially for multi-framework projects)
-   Differentiate various `TargetFrameworks` of a project
-   Provide a complete overview of the project’s outputs
-   Keep you updated on the build progress


![aa](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1rrwbaabzke32adf2p8.gif)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

To enable the new output, use the `/tl` flag along with options like `auto`, `on`, or `off`. With this logger turned on, you’ll see different phases (e.g., restoration and construction) and built projects with MSBuild targets and timings. This extra info helps you understand the build process and where to start exploring further.

As projects get built, a new “build completed” section will appear, showing:

-   Project name
-   Target Framework
-   Main compilation output
-   Compilation diagnostics


![aa](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mflzx0xcan5pabtmy8d.gif)
_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

Now, both the project and any errors stand out, making it easier to spot issues.

## SDK: Tweaked Simplified Output Path

-   [Design Proposal](https://github.com/dotnet/designs/pull/281)
-   [SDK Pull Request](https://github.com/dotnet/sdk/pull/31955)

Remember the [Preview 3](https://www.bytehide.com/blog/dotnet-8-preview-3/) simplified output path? Microsoft took your feedback onboard, and now we have some updates:

-   Default path changed from `.artifacts` to `artifacts`
-   Project file usage no longer supported
-   Easier activation with `buildprops` template for `dotnet new`

Why these changes? Most users wanted better visibility on Unix-based systems, so the `.` was dropped from the folder name. Also, using `artifacts` as the root path resolved .gitignore support and .NET SDK file globbing issues.

To try this updated feature, simply run `dotnet new buildprops --use-artifacts` and you’re good to go!

## Template Engine: Boosted Security with NuGet.org Packages

There is now a new security measure in the dotnet template engine. What’s new? Here’s what’s new:

-   Blocking `http:\\` feed package downloads (override with `--force`)

![a](https://www.bytehide.com/blog/wp-content/uploads/2023/05/securetemplate1.png)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

-   Warning users about template package vulnerabilities

![a](https://www.bytehide.com/blog/wp-content/uploads/2023/05/securetemplate2.png)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

-   Adding info on NuGet.org [prefix reserved](https://learn.microsoft.com/nuget/nuget-org/id-prefix-reservation) templates
![a](https://www.bytehide.com/blog/wp-content/uploads/2023/05/securetemplate3.png)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

-   Template package owner information

![a](https://www.bytehide.com/blog/wp-content/uploads/2023/05/securetemplate4.png)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

## NuGet: Package Signing Verification on Linux, Anyone?

Hey C# devs, guess what? Now, NuGet will start to [verify signed packages](https://learn.microsoft.com/nuget/reference/signed-packages-reference) on Linux by default! Yeah, that’s right! It’s been happening on Windows already, but macOS is still missing out.

For most of you Linux folks, no biggie, right? But hold on, if you’ve got a root certificate bundle chilling at `/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem`, you might run into some trust issues and bump into [NU3042](https://learn.microsoft.com/nuget/reference/errors-and-warnings/nu3042). Oops! Wanna skip the verification part? No problemo! Just set the `DOTNET_NUGET_SIGNATURE_VERIFICATION` environment variable to `false`. Easy peasy!

## NuGet: Checking Package Dependencies for Security Flaws

-   [GitHub Issue](https://github.com/NuGet/Home/issues/8087)
-   [Git](https://github.com/NuGet/Home/pull/12310)[Hub Pull Request](https://github.com/NuGet/Home/pull/12310)

Enable NuGet security auditing, and `dotnet restore` will generate a report of security vulnerabilities, including the impact, severity, and affected package versions. To activate this feature, add `<EnablePackageVulnerabilityAnalysis>true</EnablePackageVulnerabilityAnalysis>` to your project file or `Directory.Build.props`.

## Supercharge Your Security with Auditing

Ready to boost your project’s security? Now you can opt for security audit reports! You can achieve this by adding the next MSBuild property to your `.csproj` or MSBuild file:

```csharp
<NuGetAudit>true</NuGetAudit>
```

And bam! You’re in. And hey, don’t forget to define NuGet.org as one of your package sources to access the juicy vulnerability dataset:

```csharp
<packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
```

## Handle Vulnerable Packages like a Pro

Adding a package with known vulnerabilities? No worries, `dotnet restore` has your back with a heads-up warning! Run `dotnet restore`, and you’ll get warnings for each affected package and advisory. Neat, huh?

Oh, and check out these warning codes for different severity levels:

![a](https://miro.medium.com/v2/resize:fit:700/1*eeeP7mHcWc-npVjVR1djTA.png)

_Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-4/)_

Want more control? Set the `<NuGetAuditLevel>` MSBuild property to your desired level. Choose from `low`, `moderate`, `high`, and `critical`. So, if you’re into `moderate`, `high`, and `critical` advisories, check this example:

```csharp
<NuGetAuditLevel>moderate</NuGetAuditLevel>
```

## Embrace the Power of UTF8

Now, the new `IUtf8SpanFormattable` interface, which is like its buddy `ISpanFormattable`, but for UTF8 and `Span<byte>`. Sound cool? It gets better. It’s now implemented on all primitive types using shared logic, so you can format directly to UTF8 from various types like `Byte`, `DateTime`, and even `Rune`.

Plus, the new `Utf8.TryWrite` methods are the UTF8 versions of `MemoryExtensions.TryWrite`. You can use interpolated string syntax to format complex expressions directly into a span of UTF8 bytes. Sweet!

## Time Traveling Testers, Rejoice!

Introducing the `TimeProvider` abstract class! Mock time like a boss in your test scenarios, even mock Task operations too. Grab local and UTC time, measure performance with timestamps, and create timers effortlessly.And the icing on the cake? There’s a netstandard 2.0 library called `Microsoft.Bcl.TimeProvider` for supported .NET Framework versions and earlier .NET editions.

## Unleash the Power of Vector512 and AVX-512

.NET 8 is all about that SIMD life with `System.Runtime.Intrinsics.Vector512<T>` and AVX-512 for x86/x64 hardware. Preview 4 serves up 512-bit vector operations, 16 bonus SIMD registers, and extra instructions for 128-bit, 256-bit, and 512-bit vectors. Curious if your hardware’s up for it? Just check `Vector512.IsHardwareAccelerated`!

## AOT All the Way!

Did you know? .NET 8 Preview 4’s default `console` template is AOT-ready! Run `dotnet new console --aot` and voilà, an AOT-compiled project is yours. Choose an [optimization preference](https://learn.microsoft.com/dotnet/core/deploying/native-aot/optimizing) like Speed or Size to hit that sweet spot.

## Keep Your Linux Game Strong

.NET 8 Preview 4 updates the [supported Linux distro versions](https://github.com/dotnet/core/blob/main/release-notes/8.0/supported-os.md#linux) and has its sights set on Ubuntu 16.04 for all architectures. Preview 5 plans to upgrade the .NET 8 Linux build with [cla](https://www.phoronix.com/news/LLVM-16.0-Released)[ng 16](https://www.phoronix.com/news/LLVM-16.0-Released) to maintain the Linux love for Arm32, Arm64, and x64 architectures.

## System.Text.Json: Deserialize Like a Pro

Another feature is, System.Text.Json unlocks deserialization onto read-only fields and/or properties. Just tweak the `PreferredObjectCreationHandling` on `JsonSerializerOptions` like this:

```csharp
JsonSerializerOptions options = new()
{
  PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate
};
```

This makes deserialization more efficient and friendly with read-only properties or fields.

## System.Text.Json: Unleash the Power!

In .NET 8 Preview 4, System.Text.Json levels up with new features like `IsReflectionEnabledByDefault`, `TypeInfoResolverChain`, and support for unspeakable types.

Plus, it says bye to `JsonSerializerOptions.AddContext` and welcomes a `Try-` version of `GetTypeInfo`. All these upgrades make JSON handling even more powerful and convenient.

## Codegen: Consecutive Register Allocation FTW

.NET 8 Preview 4 introduces “consecutive register” allocation in RyuJIT’s register allocator. The algorithm’s been tweaked to make this happen, making your codegen for Arm64 even more efficient.

## Debugging on Visual Studio for Mac Just Got Better!

.NET 8 Preview 4 delivers a new and improved debugging experience for Visual Studio for Mac users. The new debugging engine boosts performance and reliability, with faster stepping, a more precise call stack, and smoother handling of multi-process and multi-threaded debugging.

Note that this new engine is only available for .NET Core 3.1, .NET 5, and later projects.

## Microsoft.Extensions.Logging Updates

The Microsoft.Extensions.Logging library in .NET 8 Preview 4 now supports scopes and structured logging. Create log scopes using the `LoggerMessage.DefineScope` method to capture structured data without any additional memory allocation.

The new API is also compatible with the `Log` methods provided by the `LoggerMessage` class for a consistent logging experience.

## Conclusion

So there you have it, folks! We’ve dived into some thrilling new features and enhancements in .NET 8 Preview 4, shining the spotlight on System.Text.Json improvements, register allocation in codegen, and unspeakable types support. These updates showcase Microsoft’s dedication to equipping devs with potent and streamlined tools for crafting top-notch applications. Happy coding!