C# 12, a planned upgrade to Microsoft’s signature object-oriented language for .NET development, introduces code rerouting to improve compilation, in the latest preview. The update also features access to inline arrays.
Available in Visual Studio 17.7 Preview 3 and .NET 8 Preview 6, both published July 11, the latest C# 12 preview includes features intended to enable future performance enhancements. Among these is an experimental feature dubbed interceptors that allows generators to reroute code, to provide context-specific optimization. C# 12 is set to ship as a final, production release with .NET 8 in the November time frame.
[ Coding with AI: Tips and best practices from developers ]
Interceptors are intended for advanced scenarios, allowing for better ahead-of-time (AOT) compilation. Attributes specify the actual source code location so interceptors generally only appropriate for source generators. As an experimental part of .NET 8, interceptors could be changed or removed at some point and thus should not be used in production.
Another capability, the nameof expression, which produces the name of a variable type or member as a string constant, has been enhanced to work in more places. The nameof keyword now works with member names including initializers, on static members, and in attributes.
Another C# 12 feature, the InlineArrayAttribute, was introduced to the runtime in a prior .NET 8 preview. This capability identifies a type that can be treated as a contiguous sequence of primitives for efficient, type-safe, overrun-safe, indexable and sliceable inline data. It is described as an advanced feature that will be used primarily by the compiler, .NET libraries, and other libraries. Inline arrays are used by .NET libraries to improve the performance of applications.
Microsoft previously previewed other C# 12 features including optional parameters for lambda expressions, the ability to alias any type, and primary constructors, for adding parameters to a class declaration.