You can get C# 12 by installing the latest version of Visual Studio or the latest .NET SDK. To try out C# 12 features, you should set the language version of your project to “preview::
1 | <PropertyGroup> <LangVersion>preview</LangVersion> </PropertyGroup> |
name : member access to the instance
Word name Now works with member names, including initials, on static members and in attributes:
1 | internal class NameOf { public string S { get; } = ""; public static int StaticField; public string NameOfLength { get; } = nameof(S.Length); public static void NameOfExamples() { Console.WriteLine(nameof(S.Length)); Console.WriteLine(nameof(StaticField.MinValue)); } [Description($"String {nameof(S.Length)}")] public int StringLength(string s) { return s.Length; } } |
inline arrays
I'InlineArrayAttribute It was introduced at runtime in an earlier preview of .NET 8. This is an advanced feature that will mainly be used by the compiler, .NET libraries, and a few others. The attribute specifies a type that can be treated as a contiguous sequence of inline data primitives that are efficient, secure, overflow-safe, or hashable. .NET libraries improve the performance of applications and tools that use inline arrays.
The compiler creates different ILs to access the built-in arrays. This results in some limitations, such as the inability to support list styles. In most cases, you can access tables online in the same way as other tables. The difference in UI allows to get the performance without modifying the code:
1 | private static void InlineArrayAccess(Buffer10<int> inlineArray) { for (int i = ; i < 10; i++) { inlineArray[i] = i * i; } foreach (int i in inlineArray) { Console.WriteLine(i); } } |
Most people will use tables online rather than create them. But it's good to understand how things work. Online tables are fast because they are based on a precise layout of a specific length. An inline array is a type that contains one field and is marked with the attribute InlineArrayAttribute which specifies the length of the array. In the type used in the previous example, the implementation creates storage for exactly ten items in buffer 10
1 | [System.Runtime.CompilerServices.InlineArray(10)] public struct Buffer10<T> { private T _element0; } |
objectors or objectors
This preview introduces an experimental feature called objectors. It is intended for advanced scenarios, allowing in particular to optimize predictive compilation (before AOT time). As a beta part of .NET 8, it may be changed or removed in a future release. So it should not be used in a production context.
Interceptors allow calls to a particular method to be redirected to different code. Attributes specify the physical location of the source code, so interceptors are generally only suitable for source generators.
Since interceptors are an experimental feature, you will need to enable them explicitly in your project file:
1 | <PropertyGroup> <Features>InterceptorsPreview<Features> </PropertyGroup> |
Interceptors allow you to create interesting code patterns. Here are some examples:
- Calls known at compile time, eg Regex.IsMatch(@"a + b +") With a static pattern, it can be intercepted in order to use statically generated code for optimization suited to AOT.
- ASP.NET Minimal API calls like app.MapGet("/products", handler: (int? page, int? pageLength, MyDb db) => {...}) It can be intercepted to register a statically generated thunk that calls the user's handler directly, bypassing alloc and direct.
- In vectorization, where foreach loops contain calls to user methods, the compiler can rewrite the code to check for and use the relevant gems at runtime, but revert to the original code if those gems are not available.
- Fixed precision of dependency diagram for dependency injection, where Provider.Register.MyService> () can be intercepted.
- Calls to query providers to provide translation into another language (such as SQL) can be intercepted at compile time, rather than evaluating the translated expression trees at runtime.
- Serializers can create a type-specific sequence (d) based on the concrete type of calls such as sequence
() all at compile time.
Most programmers won't use Interceptors directly, but Microsoft hopes they play an important role in making your applications run faster and easier to deploy. Interceptors are expected to remain experimental in C# 12 / .NET 8 release and may be included in a future release of C#.
source :Microsoft
And you?
What do you think of these new features in C# 12?
See also:
A first look at C# 11 features with null parameters check is available,
and list styles
Microsoft is previewing three new features in C# 12
Including initial constructors for classes or structs and alias definition for all types
“Evil thinker. Music scholar. Hipster-friendly communicator. Bacon geek. Amateur internet enthusiast. Introvert.”