.NET 7 Preview 5 Improves Generic Math, Observability, and More – Visual Studio Magazine


.NET 7 Preview 5 Improves Generic Math, Observability, and More

Microsoft delivered the fifth preview of .NET 7 on its way to its debut in November, with improvements to generic math, observability, and more.

Generic math is so important that it warranted its own blog posts last year as a preview feature and last week. It helps .NET developers leverage static APIs, including operators, from their generic code.

Or, as last week’s post put it: “Generic Math combines the power of generics with a new feature known as static virtuals in interfaces to allow .NET developers to take advantage of static APIs, including operators, from generic code. This means you get all the power of generics, but now with the ability to constrain the input to number types, so you no longer have to write or maintain many nearly identical implementations just to take supports multiple types. It also means that you have access to all your favorite operators and can use them from generic contexts. That is, you can now have static T Add(T left, T right) where T : INumber => left + right; whereas before it would have been impossible to define it.”

Generic Math especially benefits API authors, helping them simplify their code base. Other developers will also benefit, although more indirectly, as the APIs consumed will begin to support more types without requiring all numeric types to be explicitly supported.

“In .NET 7, we’ve made implementation improvements and responded to community feedback,” Angelos Petropoulos, product manager for the .NET team, said in a June 14 announcement post. He pointed to last week’s post (linked above) for more information.

The goal of observability, meanwhile, is to help developers better understand the state of their applications as scale and technical complexity increase, Petropoulos said. The previous link points to a 2021 article on OpenTelemetry .NET reaching version 1.0. OpenTelemetry is described as “a collection of tools, APIs, and SDKs. Use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help analyze performance and behavior of your software”. It encompasses:

  • A vendor-neutral specification for observability telemetry (distributed tracing, metrics, etc.)
  • API packages that implement public interfaces used for instrumentation
  • SDKs used by applications to configure instrumentation and interfaces allowing plugin authors to write exporters
  • Exporters that allow you to send your data to the telemetry backend of your choice

.NET 7 Preview 5 improvement is to expose performance ActivityEvent and ActivityLink tags the enumeration methods, which are explained here. The goal is to eliminate the need for reflection or broadcast and to improve performance when accessing tags associated with an event or link.

“The exposed methods can be used in performance-critical scenarios to enumerate Tag objects without any additional allocation and with fast access to items,” Petropoulos said.

Regular expression functionality has also been improved, in particular regarding a new RegexGenerator source generator, which helps developers statically generate regular expressions at compile time, improving performance, as explained in this post from last month . To help you out, in Preview 5 the team added a Roslyn Analyzer and Fixer:

  • Analyzer: This will look for uses of Regex which could instead be converted to use the source generator RegexGenerator. The parser can detect uses of Regex constructors and static Regex methods that meet the following criteria:
    • The supplied parameters have a value known at compile time. The output of the source generator depends on these values, so they must be known at compile time.
    • They are part of an application that targets .NET 7. The new analyzer comes in the .NET 7 targeting pack and only applications targeting .NET 7 are eligible for this analyzer.
    • The LangVersion (read more) is greater than 10. Currently the regex source generator requires LangVersion to be set to preview.
    The new analyzer in action in Visual Studio
    [Click on image for larger view.] The new analyzer in action in Visual Studio (source: Microsoft).
  • Code corrector: This suggests a RegexGenerator source generator method and gives developers the option to override the default name, after which it replaces the original code with a call to the new method.
    The new code fixer in action in Visual Studio
    [Click on image for larger view.] The new code fixer in action in Visual Studio (source: Microsoft).

Other Preview 5 highlights include:

  • System.Text.Json: This now supports serializing and deserializing polymorphic type hierarchies using attribute annotations.
  • ML.NET Text Classification API: It is the process of applying labels or categories to text in machine learning projects. “We are excited to introduce the ML.NET Text Classification API, an API that makes it easier for you to train custom text classification models and brings the latest state-of-the-art deep learning techniques for language processing natural to ML.NET,” said Petropoulos.
  • System.Reflection performance improvements when calling members: The overhead of using reflection to invoke a member (whether a method, constructor, or property gettersetter) has been significantly reduced when the invocation is performed multiple times on the same member. The team achieved 3-4x faster performance gains by reducing the overhead of using reflection to invoke a member, such as a method, constructor, or property getter/setter.
  • System.IO.Stream: The team added new methods to the database System.IO.Stream to classify — ReadExactly and ReadAtLeast – to correct common errors during use Stream.Read(). The specific problem is that Read() may return less data than is available in the Stream and less data than the passed buffer. “And even for programmers who are aware of it, having to write the same loop every time they want to read from a stream is annoying,” Petropoulos said.

All of the above and more are further explained in the announcement post, along with a bunch of other fixes, optimizations, and other tweaks.

About the Author

David Ramel is an editor and writer for Converge360.

Briana R. Cross