Working with Preview Features

Learn about preview features, allowing developers to explore and provide feedback on upcoming functionalities like C# 11 features.

It is a challenge for Microsoft to deliver some new features that have cross-cutting effects across many parts of .NET, like the runtime, language compilers, and API libraries. It is the classic chicken and egg problem. What do we do first?

From a practical perspective, it means that although Microsoft might have completed most of the work needed for a feature, the whole thing might not be ready until very late in their now annual cycle of .NET releases, too late for proper testing in “the wild.”

Preview features in .NET 6 onward

From .NET 6 onward, Microsoft will include preview features in general availability (GA) releases. Developers can opt into these preview features and provide Microsoft with feedback. In a later GA release, they can be enabled for everyone.

Preview features vs. preview versions

It is important to understand that this topic refers to preview features, which are different from preview versions of .NET or Visual Studio 2022. Microsoft releases preview versions of Visual Studio and .NET during development to gather feedback from developers, followed by a final General Availability (GA) release. At GA, the feature becomes available to everyone. Before that, the only way to access the new functionality was by installing a preview version. In contrast, preview features are included in GA releases but must be explicitly enabled.

Exploring C# 11 features in .NET 6

When Microsoft released .NET SDK 6.0.200 in February 2022, it included the C# 11 compiler as a preview feature. This meant that .NET 6 developers could optionally set the language version to preview and then start exploring C# 11 features like raw string literals and the required keyword.

Note: Preview features are not supported in production code. Preview features are likely to have breaking changes before the final release. Enable preview features at your own risk.

Requiring preview features

The [RequiresPreviewFeatures] attribute indicates assemblies, types, or members that use and, therefore, require warnings about preview features. A code analyzer then scans for this assembly and generates warnings if needed. If our code does not use preview features, we will not see any warnings. If we use any preview features, our code should warn consumers of our code that we use preview features.

Enabling preview features

In the project file, add an element to enable preview features and an element to enable preview language features, as shown highlighted in the following markup:

Get hands-on with 1400+ tech skills courses.