0

.NET 10 & C# 14: Code ít hơn, Performance tốt hơn

.NET 10 và C# 14 ra mắt, mang cải tiến thực tế giúp code sạch hơn, data processing nhanh hơn. Không như vài version trước push concept trừu tượng, lần này giải quyết pain point thực tế – pattern verbose và perf bottleneck thường gặp.

image.png

Dưới đây là core technique + feature giúp nhanh chóng onboard version mới.


C# 14: Language feature chính

Null-Conditional Assignment (??=) chain mở rộng

Trước chỉ work direct variable. C# 14 extend qua object chain: short-circuit nếu parent null, assign nếu target property null. Perfect cho deep config/DTO.

public void InitNotification(UserProfile? userProfile)
{
    // Chỉ tạo nếu userProfile tồn tại VÀ Settings null
    // Không cần if (userProfile?.Settings == null) verbose
    userProfile?.Settings ??= new UserSettings();
}

field keyword: Auto-property backing field access

Lâu mong đợi: access implicit backing field trong property accessor không cần declare private field manual.

public int MaxConnections
{
    get => field;
    set 
    {
        if (value <= 0)
            throw new ArgumentOutOfRangeException(nameof(value), "Phải > 0");
        field = value;
    }
}

Lambda perf unlock: ref, out, in support

High-perf scenario (HFT, game physics) giờ zero-copy inline logic trong lambda.

struct Position { public double X, Y; }

var move = (ref Position pos, double deltaX, double deltaY) => 
{
    pos.X += deltaX;
    pos.Y += deltaY;
};

var pos = new Position { X = 10, Y = 20 };
move(ref pos, 5, 5); // pos.X = 15, không copy overhead

.NET 10: Framework & Runtime upgrade

Single-File Apps (Không cần .csproj)

Script-like usage không project file. Tốt cho ops script/quick algo.

// clean_logs.cs
using System.IO;

var logPath = "./logs";
if (Directory.Exists(logPath))
{
    var count = Directory.GetFiles(logPath).Length;
    Console.WriteLine($"Tìm thấy {count} log file, cleaning...");
}

// Run: dotnet run clean_logs.cs

EF Core: Native LeftJoin + JSON update

LeftJoin: Không còn GroupJoin().SelectMany() awkward.

var list = context.Students
    .LeftJoin(context.Scholarships,
        s => s.Id, 
        sch => sch.StudentId,
        (student, scholarship) => new 
        { 
            Name = student.Name, 
            Amount = scholarship?.Amount ?? 0
        })
    .ToList();

JSON partial update: Update specific JSON property không đọc full entity.

context.Users.Where(u => u.Id == 1)
    .ExecuteUpdate(setters => setters
        .SetProperty(u => u.Config.Theme, "Dark"));

Minimal API native validation

API dev clean hơn, built-in declarative validation.

app.MapPost("/user", (UserDto user) => ProcessUser(user))
   .WithParameterValidation(); // Auto validate [Required] etc.

Modern dev environment management

.NET iterate nhanh. Maintain 10 năm .NET Framework legacy bên .NET 10 + database/SDK → environment pollution + conflict.

image.png

ServBay giải quyết như integrated dev environment tool với .NET support full:

Full version + legacy coexist

  • .NET 2.0 → 10.0
  • Native Mono 6 cho Unity/Xamarin/legacy .NET

One-click supporting service

  • SQL: PostgreSQL, MySQL, MariaDB
  • NoSQL: Redis, Memcached, MongoDB
  • One-click start/stop, không Docker hassle

Multi-language isolation

  • Rust, Node.js, PHP cùng .NET
  • Per-project isolation tránh global upgrade break project khác

Kết luận

.NET 10 + C# 14 hướng tới code ít hơn, capability nhiều hơn. Từ field simplify property đến EF Core handle complex SQL, giảm cognitive load mạnh.

Kết hợp integrated dev environment tool như ServBay loại bỏ setup noise. Syntax mới improve code quality; tooling đúng đảm bảo focus business logic thay vì environment wrestling.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí