Microsoft is known for constantly upgrading its products to ensure its customers avail the best of the technologies and features.
Today .NET 7 is (almost) a reality and it seems that Microsoft is now confirming many of the rumors that we are in front of the fastest .NET version ever: .NET 7.
.Net has gained the reputation of being the most flexible and efficient software development framework since its inception. Microsoft has been continually innovating and evolving the .Net Framework to make it faster and better.
Microsoft has recently released the third and fourth preview version of .NET 7, along with .NET Core 7 Preview 1 and Entity Framework 7 Preview 1. Microsoft claimed that these fresh versions of .NET bring improved and strong support for cloud-native containers and applications.
Let’s take a look at the latest features and improvements released by Microsoft.
APIs for Tar Archives
The framework gets the new assembly System.Formats.Tar. This cross-platform API allows you to read, write, archive, and extract tar archives.
It contains methods such as TarFile.CreateFromDirectory and Tar-File.ExtractToDirectory, for which there are numerous overloads to address either archives via filenames or via streams.
Here is an example:
TarFile.CreateFromDirectory( sourceDirectoryName: "sampleDir/", destinationFileName: "example.tar", includeBaseDirectory: true ); TarFile.ExtractToDirectory( sourceFileName: "sample.tar", destinationDirectoryName: "testDir/", overwriteFiles: true );
The APIs also allow you to open an archive and go through its contents, either to read the entries one by one or to write them.
New LINQ Updates
The new methods Order and OrderDescending sort an IEnumerable based on the type T. The new feature simplifies your code for sorting:
var unsortedArray = new[] { 77, 13, 19 }; var sortedArrayAsc = data.Order(); var sortedArrayDesc = data.OrderDescending();
At above interfaces applies for IQueryable also 🙂
Introducing New DataTypes
With .NET 6, some new data types have already been added:
- DateOnly: “Represents dates with values ranging from January 1, 0001 through December 31, 9999 A.D. (C.E.) in the Gregorian calendar.”
- TimeOnly: “Represents a time of day, as would be read from a clock, within the range 00:00:00 to 23:59:59.9999999.”
- Half: “Represents a half-precision floating-point number.”
- Int128
- UInt128
System.Text.Json
Polymorphism
System.Text.Json now supports serializing and deserializing polymorphic type hierarchies using attribute annotations:
[JsonDerivedType(typeof(Derived))] public class Base { public int X { get; set; } } public class Derived : Base { public int Y { get; set; } }
This configuration enables polymorphic serialization for Base
, specifically when the runtime type is Derived
:
Base value = new Derived(); JsonSerializer.Serialize<Base>(value); // { "X" : 0, "Y" : 0 }
Note that this does not enable polymorphic deserialization since the payload would be roundtripped as Base
:
Base value = JsonSerializer.Deserialize<Base>(@"{ ""X"" : 0, ""Y"" : 0 }"); value is Derived; // false
Using Type Discriminators
To enable polymorphic deserialization, users need to specify a type discriminator for the derived class:
[JsonDerivedType(typeof(Base), typeDiscriminator: "base")] [JsonDerivedType(typeof(Derived), typeDiscriminator: "derived")] public class Base { public int X { get; set; } } public class Derived : Base { public int Y { get; set; } }
Which will now emit JSON along with type discriminator metadata: which can be used to deserialize the value polymorphically:
Base value = new Derived(); JsonSerializer.Serialize<Base>(value); // { "$type" : "derived", "X" : 0, "Y" : 0 }
Type discriminator identifiers can also be integers, so the following form is valid:
[JsonDerivedType(typeof(Derived1), 0)] [JsonDerivedType(typeof(Derived2), 1)] [JsonDerivedType(typeof(Derived3), 2)] public class Base { } JsonSerializer.Serialize<Base>(new Derived2()); // { "$type" : 1, ... }
Closing
We appreciate you for taking some time and reading this blog. Please give .NET 7 Preview 5 a try and tell us what you think!
Stay tune we will bring more .Net 7 feature in coming blogs.
Check original Source: .NET 7 Preview 5