On October 11th, 2022, Microsoft released.NET 7 Release Candidate 2. It is supported in production as the last release candidate (RC) for.NET 7.

Visual Studio 17.4 Preview 3 has been used to test.NET 7 Release Candidate 2. If you want to test out.NET 7 with products from the Visual Studio family, we advise using the preview channel builds. We advise using the most recent Visual Studio 2022 for Mac preview if you use macOS.

We want to highlight the key concepts of .NET 7 in this post and give you resources to delve further into the specifics. For a more thorough list of each enhancement and feature added to .NET 7 Release Candidate 2 you can check the previous posts from .NET’s official website.

C# 11

The most recent version of C#, C# 11, is now accessible in.NET 7.

They openly create and develop C#. To view the most recent C# feature requests and meeting notes, connect to them on the CSharpLang repository. As soon as work is scheduled, you can check the Feature Status page to see how things are going. Create a C# project and set the LangVersion property to Preview to experiment with the C# 11 preview features.

Libraries & SDK

The.NET libraries are always getting better. New APIs with completely new features are frequently added. Existing APIs are receiving performance upgrades, which will benefit you if you simply upgrade. New libraries are being created to help you with your daily tasks. Every new version of.NET brings a wealth of enhancements to the.NET SDK, which contains the essential tools for designing, developing, and maintaining.NET projects. Numerous improvements were already present in earlier preview. To read more about new SDK features, see the following:

Re-enable Reflection Fallback For System.Text.Json source generation

.NET 7 introduced an intentional breaking change which removes silent fallback to reflection-based serialization in System. generators for Text.Json sources. Early customer feedback indicates that a significant number of users have (mostly unintentionally) come to depend on the fallback behavior.

Despite the fact that a workaround for the breaking change has been documented, it still calls for a code change, which may not always be possible. Starting with.NET 7 RC 2, you can use the provided AppContext compatibility switch to globally re-enable reflection fallback. To enable reflection fallback once more for all source-generated contexts in your app, add the following entry to the project file for your application:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.EnableSourceGenReflectionFallback" Value="true" />
</ItemGroup>

See the post on .NET runtime configuration settings for more details on using AppContext switches.

Implementation of Generic Math interfaces correctly

The.NET runtime ensures that user code correctly implements the.NET Generic Math interfaces that use the Curiously Recurring Template Pattern (CRTP). It specifically issues a warning if a type that implements the CRTP pattern and the.NET Generic Math interfaces fails to fill the generic type parameter with the type itself.

For example:

public readonly struct DateOnly : IParsable<DateOnly> // correct implementation of IParsable<TSelf> interface
{ ... }
public readonly struct MyDate : IParsable<DateOnly> // Warns: "The 'IParsable<TSelf>' requires the 'TSelf' type parameter to be filled with the derived type 'MyDate' " the type parameter TSelf
{ ... }

Some built in operators added in .NET 7 for System. System and IntPtr. In.NET 6 and earlier, UIntPtr behave differently from user-defined operators. Some operators that used to throw when the context was unchecked while overflowing are now only allowed to do so when wrapped in checked context, and some operators that did not previously throw in checked context are now only allowed to throw when the context is unchecked. The analyzer alerts it when it finds the code that might be responsible for those behavioral changes.

For example:

checked
{
    intPtr2 = intPtr1 + 2; // Warns: "Starting with .NET 7 the operator '+' will throw when overflowing in a checked context. Wrap the expression with an 'unchecked' statement to restore the .NET 6 behavior."

    intPtr2 = intPtr1 - 2; // Warns: "Starting with .NET 7 the operator '-' will throw when overflowing in a checked context. Wrap the expression with an 'unchecked' statement to restore the .NET 6 behavior."

    void* ptr = (void*)intPtr1; // Warns: "Starting with .NET 7 the explicit conversion '(void*)IntPtr' will throw when overflowing in a checked context. Wrap the expression with an 'unchecked' statement to restore the .NET 6 behavior."

    intPtr2 = (IntPtr)ptr; // Warns: "Starting with .NET 7 the explicit conversion '(IntPtr)void*' will throw when overflowing in a checked context. Wrap the expression with an 'unchecked' statement to restore the .NET 6 behavior."
}

intPtr1 = (IntPtr)longValue; // Warns: "Starting with .NET 7 the explicit conversion '(IntPtr)Int64' will not throw when overflowing in an unchecked context. Wrap the expression with a 'checked' statement to restore the .NET 6 behavior."

int a = (int)intPtr1; // Warns: "Starting with .NET 7 the explicit conversion '(Int32)IntPtr' will not throw when overflowing in an unchecked context. Wrap the expression with a 'checked' statement to restore the .NET 6 behavior."

Support

The release of.NET 7 is covered by Standard Support. There is no change to the support duration despite the fact that this is the new name for what was previously known as Current. The Standard Support period for.NET releases with odd numbers is 18 months. The 36-month length of Long-Term Support (LTS), as well as its name, have not changed. For more information, see our.NET and.NET Core Support Lifecycle document.

Wrapping Up

.NET is loved for it’s simplicity of C# cause everything can be completed quickly and easily (in C#, everyone who can program can program). Similarly, the Jit itself is intuitive in the way it applies compiler techniques. Finally, a shoutout to SuperPMI.

Releases of .NET include products, libraries, runtime, and tooling, and represent a collaboration across multiple teams inside and outside Microsoft. Give .NET 7 Release Candidate 2 a try and tell us what you think!