.NET 7 Preview 6 Announced

In this article, we are going to discuss .NET 7 Preview 6.

Microsoft has released .NET 7 Preview 6, just one month after .NET 7 preview 5.

Below are a couple of enhancements of the .NET 7 Preview.

  1. Improvement in type converters,
  2. System.Formats.Tar APIs
  3. Add JSON contract customization
  4. Added constraints to NET template authoring
  5. Some of the performance enhancement

We can download .NET 7 Preview 6, for Windows, Linux, and macOS.

This .Net 7 preview 6 Tested on the visual studio 17.3 preview 3.

1. Type Converters

Microsoft team did some improvements in primitive types including int128, Uint128, TimeOnly, DateOnly and Half.

Type Converter's implementation is like the below

namespace System.ComponentModel {
    public class Int128Converter: System.ComponentModel.BaseNumberConverter {
        public Int128Converter() {}
    }
    public class UInt128Converter: System.ComponentModel.BaseNumberConverter {
        public UInt128Converter() {}
    }
    public class TimeOnlyConverter: System.ComponentModel.TypeConverter {
        public TimeOnlyConverter() {}
    }
    public class DateOnlyConverter: System.ComponentModel.TypeConverter {
        public DateOnlyConverter() {}
    }
    public class HalfConverter: System.ComponentModel.BaseNumberConverter {
        public HalfConverter() {}
    }
}

Explanation with Example

TypeConverter Converterint128 = TypeDescriptor.GetConverter(typeof(Int128));
Int128 ? _int128 = Converterint128.ConvertFromString("123456") as Int128 ? ;

TypeConverter ConverterUInt128 = TypeDescriptor.GetConverter(typeof(UInt128));
UInt128 ? _uint128 = ConverterUInt128.ConvertFromString("123456")

TypeConverter ConvertertimeOnly = TypeDescriptor.GetConverter(typeof(TimeOnly));
TimeOnly ? _time = ConvertertimeOnly.ConvertFromString("12:00:00") as TimeOnly ? ;

TypeConverter ConverterdateOnly = TypeDescriptor.GetConverter(typeof(DateOnly));
DateOnly ? _date = ConverterdateOnly.ConvertFromString("2022-14-07") as DateOnly ? ;

TypeConverter Converterhalf = TypeDescriptor.GetConverter(typeof(Half));
Half ? _half = Converterhalf.ConvertFromString(((Half)(3)).ToString()) as Half ? ;

2. System.Formats.Tar APIs

Microsoft team did some improvement in System.Formats.Tar APIs assembly. This assembly was introduced in Preview 4. It helps us in manipulating TAR archives.

This enhancement was done to cover a couple of special cases

  1. Writing and reading changes -- The Format property was removed from TarReader because no archive is expected to have all its entries in a single format.
  2. Entry format, not archive format —- The team discovered that entries of different formats can be intermixed in a single TAR archive, so the LarFormat enum was renamed to TarEntryFormat.
  3. Global Extended Attributes specialized class — a new class was added to describe a GEA entry.

3. JSON contract customization

Microsoft team did an improvement in JSON contract customization. In some cases, developers need to serialize or de-serialize the JSON, which they don't want to, but they do because those JSON comes from an external library.

JSON Contract customization allows developers more control over serialized or deserialized.

Now for JSON customization, developers no need to use JsonSerializerOptions.TypelnfoResolver and require assigning resolver.

Developers can use JSON customization by using DefaultJsonTypeInfoResolver and adding their modifier, all modifiers will be called serially,

JsonSerializerOptions Jsonoptions = new() {
    TypeInfoResolver = new DefaultJsonTypeInfoResolver() {
        JsonModifiers = {
            (JsonTypeInfo jsonTypeInfo) => {
                // your modifications here, i.e.:
                if (jsonTypeInfo.Type == typeof(int)) {
                    jsonTypeInfo.NumberHandling = JsonNumberHandling.AllowReadingFromString;
                }
            }
        }
    }
};

Employee employee = JsonSerializer.Deserialize < Employee > (@ "{"
    "Name"
    ":"
    "DotNetOffice"
    ","
    "Address"
    ":"
    "India"
    "}", Jsonoptions);
Console.WriteLine($ "({Employee.Name },{ Employee.Address})");

public class Employee {
    public string Name {
        get;
        set;
    }
    public string Address {
        get;
        set;
    }
}
  • When the type is not managed in code should return null.
  • Writing custom resolver by using System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.
  • IJsonTypeInfoResolver can be combined with others resolver which will return first non-null value. example JsonTypeInfoResolver.Combine(new MyCustomeResolver(), new DefaultJsonTypeInfoResolver()).

4. Added constraints to .NET template authoring

Microsoft team did enhancement in .net template authoring using three ways

MultiChoice Parameters:- when we work with choice parameters, now developers can declare more than one value in a single selection, just like Flags-style enum.

For example,

+availing in multiple forms of authentication on the web template.

+ Choosing multiple target platforms (ios, android, web) at once in the Maui templates.

you can follow the documentation for multi-choice parameters for more details.

> Constraints — .NET preview 6 introduces the Constraints to the .NET template

it allows us to define the context in the template, which helps the developer in the

template engine, to identify which template should show in commands.

for example, dotnet new list.

.NET 7 Preview 6 supports three kinds of constraints:

  • Operating System constraints limit templates based on the user OS.
  • Installed Workloads constraints require the specified .NET SDK workload which is installed before the template will be available.
  • Template Engine Host constraints limit templates based on which host is executing the Template Engine, which is usually the .NET CLI itself, or embedded scenarios like the New Project Dialog in Visual Studio.
  • Exit codes unification and reporting -- .NET 7 preview 6 release unifies exit codes reported by the Template Engine, it helps users who depend on scripting in shells to have more consistent error-handling experience. A and also, errors reported by the .NET CLI now include a link so developers can find more information on each exit code.

.NET 7 preview 6 contains some performance enhancement with respect to CodeGen as well.

  • CodeGen: the development team helped out with a couple of community PRs. The Changes affected dynamically PGO (profile-guided optimization), loop optimizations, and Arm64, along with other more general optimizations.

For more information, you can follow the Link