.NET  

.NET 8 & C# 12 New Features

Introduction

.NET 8 (the latest Long-Term Support release) and C# 12 introduce significant improvements for performance, productivity, and modern application development. These updates are relevant whether you're building APIs, web applications, or cloud services.

In this article, you’ll learn:

  • 5 key C# 12 language features with code examples
  • 5 major .NET 8 runtime and framework enhancements
  • How to migrate from .NET 7 to .NET 8

Part 1. Top 5 C# 12 Features

1. Primary Constructors

Constructors can now be declared directly in the class definition:

public class User(string name, int age)
{
    public string Name { get; } = name;
    public int Age { get; } = age;
}

Use Case: Reduces boilerplate code in DTOs and entity classes.

2. Collection Expressions

A unified syntax for initializing various collection types:

int[] numbers = [1, 2, 3];  
List<string> names = ["Alice", "Bob"];  
Span<char> chars = ['a', 'b', 'c'];  

Use Case: Cleaner initialization of arrays, lists, and spans.

3. Default Lambda Parameters

Lambda expressions now support default parameter values:

var greet = (string name = "Guest") => $"Hello, {name}!";
Console.WriteLine(greet()); // Output: "Hello, Guest!"

Use Case: More flexible and reusable lambda expressions.

4. Interpolated String Improvements

More efficient handling of interpolated strings:

var name = "Alice";  
string message = $"Hello, {name}!"; // More efficient than C# 11

Use Case: Improved performance in logging and string templating.

5. Alias Any Type

The using Directive can now create aliases for tuple types and other complex types:

using Point = (int X, int Y);  
Point p = (10, 20);  
Console.WriteLine(p.X); // Output: 10

Use Case: Simplifying complex type signatures in your code.

Part 2. Top 5 .NET 8 Enhancements

1. Native AOT (Ahead-of-Time) Compilation

Compile applications to native code for improved startup performance:

dotnet publish -p:PublishAot=true

Performance Benefit: Up to 60% faster cold starts in serverless environments.

2. Blazor United

A unified model for building both server-side and client-side web UI:

<Counter @rendermode="InteractiveWebAssembly" />

Use Case: Developing hybrid web applications with .NET.

3. HTTP/3 and QUIC Support in ASP.NET Core

Enable modern transport protocols for better performance:

builder.WebHost.UseQuic(); // Enable HTTP/3

Network Benefit: Improved performance for APIs and microservices.

4. New Randomness APIs

New methods for random selection and shuffling:

var items = new[] { "A", "B", "C" };  
var randomItem = Random.Shared.GetItems(items, 2); // Returns random subset

Use Case: Games, statistical sampling, and random data generation.

5. Performance Optimizations

Key runtime improvements include:

  • More efficient garbage collection

  • Optimized LINQ operations (WhereSelect)

  • Enhanced SIMD support for parallel operations

Part 3. Migrating from .NET 7 to .NET 8

  1. Update your project file:

    <TargetFramework>net8.0</TargetFramework>
  2. Review breaking changes in:

    • System.Text.Json serialization

    • IdentityModel APIs

Conclusion

.NET 8 and C# 12 deliver substantial improvements for developers building modern applications. These features enable cleaner code, better performance, and more productive development workflows.

Discussion: Which feature do you find most valuable for your projects? Share your thoughts in the comments.