Microsoft is previewing new features in C# 12, including improved InlineArrayAttribute and the introduction of Interceptors. – Developpez.com

Microsoft Introduces Three New Features in C 12 Including Primary
Visual Studio 17.7 Preview 3 and .NET 8 Preview 6 continue the evolution of C# 12. This preview includes features intended to lay the groundwork for future performance improvements. Easy access to “inline arrays” allows libraries to use them in a greater number of cases without you having to do anything. This preview introduces an experimental feature called Interceptors that allows generators to redirect code, for example to provide context-specific optimization. Finally, nameof has been 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 take advantage of C# 12 features, you need to set the language version of your project to “Preview”:

1
2
3

preview

nameof: member access to the instance

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

1
2
3
4
5
6
7
8th
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

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

The compiler creates various ILs for accessing inline arrays. This leads to some limitations, such as the inability to support list patterns. In most cases, you access online spreadsheets in the same way as any other spreadsheet. The difference in UI allows for a performance boost without changing the code:

1
2
3
4
5
6
7
8th
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 online spreadsheets instead of creating them. But it’s good to understand how things work. Online spreadsheets are fast because they are based on an exact layout with a specific length. An inline array is a single-field type and is marked with the InlineArrayAttribute attribute, which specifies the length of the array. For the type used in the previous example, execution creates space for exactly ten elements in Buffer10 due to the attribute setting:

1
2
3
4
5

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

public struct Buffer10 { private T _element0; }

Interceptor or Interceptor

This review introduces an experimental feature called Interceptors. It is intended for advanced scenarios and in particular allows an improvement in expected compilation (Ahead-of-Time-AOT). Because it is 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 certain method calls to be redirected to other code. Attributes indicate the actual location of the source code, so interceptors are generally only appropriate for source generators.

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

1
2
3

Interceptors Preview

You can use interceptors to create interesting code patterns. Here are some examples:

  • Compile-time known calls like Regex.IsMatch(@”a+b+”) with constant pattern can be intercepted to take advantage of statically generated code for AOT-friendly optimization.
  • ASP.NET Minimal API calls like app.MapGet(“/products”, handler: (int? page, int? pageLength, MyDb db) => { … }) can be intercepted To a statically generated thunk , which calls the forward directly to the user’s handler, avoiding mapping and indirection.
  • With vectorization, where foreach loops contain calls to user methods, the compiler can rewrite the code to check and use the relevant intrinsic functions at runtime, but revert to the original code if those intrinsic functions are unavailable.
  • Static resolution of dependency graph for dependency injection, where provider.Register()” can be caught.
  • Calls to query providers could be intercepted to offer translation into another language (e.g. SQL) at compile time, rather than evaluating translated expression trees at runtime.
  • Serializers could perform type-specific (D)serialization based on the concrete type of calls like Serialize() generate, all at compile time.

Most programmers won’t use interceptors directly, but Microsoft hopes they’ll play an important role in making your applications run faster and more easily deployed. Interceptors are expected to remain experimental in the C# 12/.NET 8 release and may be included in a future version of C#.

Source: Microsoft

And you ?

Tinder travaille sur un new subscription mensuel a 500 dollars What do you think of these new features in C# 12?

See also:

Tinder travaille sur un new subscription mensuel a 500 dollars Rising popularity of C# according to Tiobe Index
Who has seen it grow almost 2% in the last 12 months while C has lost almost as much in popularity?

Tinder travaille sur un new subscription mensuel a 500 dollars A first look at C# 11 features is available with the null parameter check.
And the list patterns

Tinder travaille sur un new subscription mensuel a 500 dollars Microsoft is previewing three new features in C# 12
Including primary constructors for classes or structs and alias definitions for all types