Microsoft presents an overview of new features in C# 12, including the improvement of the InlineArrayAttribute and the introduction of Interceptors.

2023-07-14 19:59:55

Visual Studio 17.7 Preview 3 and .NET 8 Preview 6 continue the evolution of C# 12. This preview includes features designed to set the stage for future performance improvements. Easy access to “Inline arrays” will allow libraries to use them in a greater number of cases, without effort on your part. This preview introduces an experimental feature called “Interceptors”, which allows generators to reroute code, for example to provide context-specific optimization. Finally, nameof is improved to work in more cases.

You can get C# 12 by installing the latest version of Visual Studio or the latest version of the .NET SDK. To experience the features of C# 12, you must set the language version of your project to “preview”:

1
2
3


preview

nameof: Member access to the instance

The nameof keyword now works with member names, including initials, on static members and in attributes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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 ou tableaux en ligne

The InlineArrayAttribute was introduced to the runtime in a previous preview of .NET 8. This is an advanced feature that will be used primarily by the compiler, .NET libraries, and a few other libraries. The attribute identifies a type that can be treated as a contiguous sequence of primitives for efficient, type-safe, and overflow-safe indexable or slicable inline data. .NET libraries improve the performance of your applications and tools that use inline arrays.

The compiler creates different ILs to access inline arrays. This results in some restrictions, such as the inability to support list patterns. In most cases, you access online tables in the same way as other tables. The difference in user interface allows to gain in performance without modifying the code:

1
2
3
4
5
6
7
8
9
10
11

private static void InlineArrayAccess(Buffer10 inlineArray)

for (int i = 0; i < 10; i++) inlineArray[i] =i*i; foreach (int i in inlineArray) Console.WriteLine(i); Most people will use inline tables rather than creating them. But it's good to understand how things work. Online tables are fast because they rely on an exact layout of a specified length. An inline array is a single-field type marked with the InlineArrayAttribute attribute that specifies the length of the array. In the type used in the previous example, the execution creates storage space for exactly ten elements in Buffer10 due to attribute parameter:

1
2
3
4
5

[System.Runtime.CompilerServices.InlineArray(10)]

public struct Buffer10

private T _element0;

Interceptors ou Intercepteurs

This overview introduces an experimental feature called interceptors. It is intended for advanced scenarios, allowing in particular to improve the anticipated compilation (ahead of time-AOT). As an experimental part of .NET 8, it may be changed or removed in a future release. It should therefore not be used in a production context.

Interceptors allow specific method calls to be redirected to different code. Attributes specify the actual location of the source code, so interceptors are generally only suitable for source generators.

Because interceptors are an experimental feature, you’ll need to explicitly enable them in your project file:

1
2
3


InterceptorsPreview

Interceptors allow you to create interesting code patterns. Here are some examples: Calls known at compile time, such as Regex.IsMatch(@”a+b+”) with a constant pattern, can be intercepted in order to use statically generated code for optimization that is favorable The AOT.ASP.NET Minimal API calls like app.MapGet(“/products”, handler: (int?page, int?pageLength, MyDb db) => … ) can be intercepted to save a thunk generated statically that calls the user’s handler directly, avoiding allocation and indirection. In vectorization, where foreach loops contain user method calls, the compiler can rewrite code to check and use the relevant at runtime, but fall back to the original code if these intrinsics are not available. Static resolution of the dependency graph for dependency injection, where provider.Register() can be intercepted. Calls to query providers might be intercepted to offer translation to another language (e.g. SQL) at compile time, rather than evaluating translated expression trees at compile time. execution. Serializers might generate type-specific (d)serialization depending on the concrete type of calls such as Serialize(), all at compile time.

Most programmers won’t use interceptors directly, but Microsoft hopes they will play an important role in making your applications run faster and easier to deploy. Interceptors are expected to remain experimental in the 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 as well :

Rise in popularity of C#, according to the Tiobe Index
Who has seen him growing nearly 2% in the past 12 months, while C has lost nearly as much popularity

A first look at C# 11 features is available with null parameter checking,
And the list patterns

Microsoft previews three new features in C# 12
Including primary constructors for classes or structs and alias definition for all types

1689367563
#Microsoft #presents #overview #features #including #improvement #InlineArrayAttribute #introduction #Interceptors

Leave a Replay