C# | Exploring .NET 9's New Developer Features
.NET 9, the latest release in Microsoft’s development platform, brings significant enhancements to improve developer productivity and code quality. This post explores the key features and syntax improvements that developers can leverage in their projects.
Collection Expressions
One of the most anticipated features in .NET 9 is the introduction of collection expressions, providing a more concise way to initialize arrays and collections.
Traditional Approach
1
2
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var array = new int[] { 1, 2, 3, 4, 5 };
New Collection Expression Syntax
1
2
3
var numbers = [1, 2, 3, 4, 5]; // Type inferred collection
int[] array = [1, 2, 3, 4, 5]; // Explicit array type
List<int> list = [1, 2, 3, 4, 5]; // Explicit list type
Primary Constructors in All Types
.NET 9 extends primary constructor support to all types, including structs and non-record classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Before .NET 9
public class Customer
{
private readonly string _name;
private readonly int _age;
public Customer(string name, int age)
{
_name = name;
_age = age;
}
}
// With .NET 9
public class Customer(string name, int age)
{
public string Name => name;
public int Age => age;
}
Enhanced Pattern Matching
Pattern matching capabilities have been expanded with new syntax options:
1
2
3
4
5
6
7
8
9
10
void ProcessValue(object value)
{
var result = value switch
{
> 100 and int n => $"Large number: {n}",
[var first, .. var rest] => $"Collection starting with {first}",
string { Length: > 10 } s => $"Long string: {s}",
_ => "Unknown value"
};
}
Interceptors
.NET 9 introduces interceptors, allowing developers to modify or intercept method calls at compile time:
1
2
3
4
5
6
[InterceptsLocation("Program.cs", line: 10, character: 12)]
static void LogMethodCall<T>(T value)
{
Console.WriteLine($"Method called with value: {value}");
// Original method call continues
}
Native AOT Improvements
Native AOT (Ahead of Time) compilation has been enhanced with:
- Broader framework support
- Improved debugging experience
- Reduced binary size
- Faster startup times
1
2
var builder = WebApplication.CreateSlimBuilder(args);
builder.AOTOptions.Enable(); // Enable AOT compilation
Extension Method Groups
A new syntax for working with extension methods as method groups:
1
2
3
4
5
// Before
var processed = items.Select(x => x.ProcessItem());
// Now
var processed = items.Select(Extensions.ProcessItem);
String Interpolation Enhancements
More powerful string interpolation capabilities:
1
2
3
4
5
6
7
var name = "Hassan";
var age = 35;
var formatted = $"""
User Profile:
Name: {name,20} // Right-aligned in 20 spaces
Age: {age:D3} // Zero-padded to 3 digits
""";
Performance Optimizations
Span Improvements
1
2
Span<char> buffer = stackalloc char[100];
var success = int.TryFormat(12345, buffer, out int charsWritten);
New Memory-Efficient Types
1
2
3
4
5
6
7
using System.Collections.Frozen;
var cityDict = new Dictionary<string, int>
{
["Tangier"] = 1,
["Casablance"] = 2
}.ToFrozenDictionary();
What next ?
.NET 9 brings substantial improvements to developer productivity and application performance. The new syntax features and optimizations make code more readable and maintainable while providing better runtime performance.
These enhancements demonstrate Microsoft’s commitment to evolving the platform based on developer feedback and modern programming practices. As developers adopt these features, they’ll find new ways to write more elegant and efficient code.