New features in .NET 9 Preview 1

14 Feb
.NETC#Coding

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

Demo

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
  });

Official documentation

1.2. New default JsonSerializerOptions.Web singleton

Demo

var car = new Car { Make = "Lamborghini", Model = "Aventador", FacturedYear = 2020 };
var json = JsonSerializer.Serialize(
  car,
  JsonSerializerOptions.Web); // `JsonSerializerOptions.Web` is new in .NET 9

Official documentation

2. LINQ

2.1. New CountBy method

Demo

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

Demo

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

Demo

// `Index` is new in .NET 9
foreach (var (index, fruit) in fruits.Index())
{
  Console.WriteLine($"{index + 1}º {fruit}");
}

Official documentation

3. Data Structures

3.1. New method Remove in PriorityQueue

Demo

var queue = new PriorityQueue<string, int>();
queue.Remove("John", out var removedItem, out var priority); // `Remove` is new in .NET 9

Official documentation

4. Cryptography

4.1. New CryptographicOperations.HashData()

Demo

var bytes = Encoding.ASCII.GetBytes("Hello .NET 9!");
var hash = CryptographicOperations.HashData(HashAlgorithmName.SHA512, bytes); // `CryptographicOperations.HashData` is new in .NET 9

Official documentation

4.2 New KMAC algorithms

Demo

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

Official documentation

We are using cookies to ensure that we give you the best experience on our website. By clicking "Accept", you consent to the use of ALL the cookies. However you may visit Cookie policy to provide a controlled consent.