New features in .NET 9 Preview 1
Welcome, welcome, welcome! It's a brand-new year, and you know what that means? Yes, it's time for a fresh .NET release from Microsoft! Keeping up with their tradition, Microsoft has rolled out the first preview of .NET 9 on February 13th. It's a standard-term support (STS) version, promising new goodies and improvements.
What’s Exciting in the New Version?
1. Serialization
1.1. New options in JsonSerializerOptions IndentCharacter
and IndentSize
var car = new Car { Make = "Lamborghini", Model = "Aventador", FacturedYear = 2020 };
var json = JsonSerializer.Serialize(
car,
new JsonSerializerOptions
{
WriteIndented = true,
IndentCharacter = '\t', // `IndentCharacter` is new in .NET 9
IndentSize = 2 // `IndentSize` is new in .NET 9
});
1.2. New default JsonSerializerOptions.Web
singleton
var car = new Car { Make = "Lamborghini", Model = "Aventador", FacturedYear = 2020 };
var json = JsonSerializer.Serialize(
car,
JsonSerializerOptions.Web); // `JsonSerializerOptions.Web` is new in .NET 9
2. LINQ
2.1. New CountBy
method
var charFrequence = "Welcome to .NET 9!"
.ToCharArray()
.Where(w => !char.IsWhiteSpace(w))
.CountBy(chars => chars); // `CountBy` is new in .NET 9
2.2. New AggregateBy
method
var aggregated = data.AggregateBy( // `AggregateBy` is new in .NET 9
entry => entry.product,
seed: 0,
(total, curr) => total + curr.quantity);
2.3. New Index
method
// `Index` is new in .NET 9
foreach (var (index, fruit) in fruits.Index())
{
Console.WriteLine($"{index + 1}º {fruit}");
}
3. Data Structures
3.1. New method Remove
in PriorityQueue
var queue = new PriorityQueue<string, int>();
queue.Remove("John", out var removedItem, out var priority); // `Remove` is new in .NET 9
4. Cryptography
4.1. New CryptographicOperations.HashData()
var bytes = Encoding.ASCII.GetBytes("Hello .NET 9!");
var hash = CryptographicOperations.HashData(HashAlgorithmName.SHA512, bytes); // `CryptographicOperations.HashData` is new in .NET 9
4.2 New KMAC algorithms
Console.WriteLine($"Kmac128 is Supported algorithms: {Kmac128.IsSupported}"); // `Kmac128` is new in .NET 9
Console.WriteLine($"Kmac256 is Supported algorithms: {Kmac256.IsSupported}"); // `Kmac256` is new in .NET 9
Console.WriteLine($"KmacXof128 is Supported algorithms: {KmacXof128.IsSupported}"); // `KmacXof128 is new in .NET 9
Console.WriteLine($"KmacXof256 is Supported algorithms: {KmacXof256.IsSupported}"); // `KmacXof256 is new in .NET 9