1.26 — Modern C# by Version (C# 8 → 14)¶
← Roadmap · Section: 1. C# Language Fundamentals Prev: 1.25 Span and Memory · Next: 2.1 Collection interfaces
Markers: ⭐ · Confidence: 🔴 · Last reviewed: —
TL;DR¶
- The C# version is tied to the .NET version. C# 12 came with .NET 8, C# 13 with .NET 9, C# 14 with .NET 10.
- The features that changed how everyday code looks: nullable reference types (8), records (9), top-level statements and file-scoped namespaces (10), required members (11), primary constructors and collection expressions (12).
- In an interview, do not recite the list. Name two or three you have actually used and say what problem they solved.
The concept¶
Interviewers ask "what's new in C#?" for one reason: to find out whether you keep up.
They are not testing whether you can recite a changelog. A candidate who names twelve features they have never used sounds worse than one who names two and explains why they mattered.
So the goal of this note is not to memorise everything. It is to recognise these features when you see them, and to have two or three you can talk about properly.
How it works¶
C# 8 (.NET Core 3.0, 2019)¶
- Nullable reference types —
string?and the warnings that go with it. The biggest change to how C# code is written. See 1.15. usingdeclarations — no braces needed. See 1.21.- Switch expressions — the
=>arm form. See 1.17. - Default interface methods — see 1.8.
IAsyncEnumerable<T>andawait foreach— see 2.17.- Ranges and indices —
array[1..^1],array[^1]for the last element.
C# 9 (.NET 5, 2020)¶
- Records — value equality and
withfor free. See 1.2. initaccessors — immutable properties that still work with object initialisers.- Top-level statements —
Program.cswithout a class or aMainmethod. - Pattern matching improvements —
and,or,not, and relational patterns. staticlambdas — compiler-enforced "captures nothing". See 1.13.
C# 10 (.NET 6, 2021)¶
- File-scoped namespaces — one less level of indentation everywhere:
- Global usings — declare a
usingonce for the entire project: Combined with<ImplicitUsings>enable</ImplicitUsings>, this is why modern files have almost nousinglines at the top. record struct— a value-type record. See 1.2.- Constant interpolated strings, and improved lambda type inference.
C# 11 (.NET 7, 2022)¶
requiredmembers — the compiler forces the caller to set them. See 1.6.- Raw string literals — no more escaping in JSON or SQL:
- List patterns —
if (arr is [1, 2, ..]). See 1.17. - Generic maths / static abstract interface members — lets you write
T Add<T>(T a, T b)whereTsupports+.
C# 12 (.NET 8, 2023)¶
- Primary constructors on classes — the biggest everyday change since records:
This removes a huge amount of boilerplate from every DI-injected service, which is most classes in an ASP.NET Core app.
// Before public class OrderService { private readonly IOrderRepository _repo; private readonly ILogger<OrderService> _logger; public OrderService(IOrderRepository repo, ILogger<OrderService> logger) { _repo = repo; _logger = logger; } } // After — identical behaviour public class OrderService(IOrderRepository repo, ILogger<OrderService> logger) { // use 'repo' and 'logger' directly anywhere in the class }
One caveat worth knowing: unlike a record's primary constructor, a class primary
constructor parameter is not a property. It is a private field the compiler creates.
It is also mutable, so it is not quite the same as readonly.
- Collection expressions — one syntax for every collection type:
- Alias any type —
using Point = (int X, int Y);
C# 13 (.NET 9, 2024)¶
paramscollections —paramsnow works withSpan<T>,List<T>and others, not just arrays. That removes an allocation in common APIs like logging.fieldkeyword (preview) — access the compiler-generated backing field in a property accessor without declaring one.- New
locktype —System.Threading.Lock, faster than locking on an object. refandunsafeallowed in more places, including iterators and async methods.
C# 14 (.NET 10, 2025)¶
Ships with .NET 10, the current LTS. Check Microsoft Learn for the confirmed feature list before an interview — this is the version most likely to be probed and the one where a stale memory shows most.
Themes to expect: the field keyword going stable, extension members beyond methods, and
further null-safety improvements.
Code¶
The same class written the old way and the modern way, which is a good thing to be able to show:
// C# 7 style
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace MyApp.Orders
{
public class OrderService
{
private readonly IOrderRepository _repo;
private readonly ILogger<OrderService> _logger;
public OrderService(IOrderRepository repo, ILogger<OrderService> logger)
{
_repo = repo ?? throw new ArgumentNullException(nameof(repo));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<OrderDto> GetAsync(Guid id, CancellationToken ct)
{
var order = await _repo.GetAsync(id, ct);
if (order == null) throw new NotFoundException(id);
return new OrderDto
{
Id = order.Id,
Total = order.Total
};
}
}
}
// C# 12 style — same behaviour
namespace MyApp.Orders; // file-scoped namespace (C# 10)
// usings are global (C# 10)
public class OrderService( // primary constructor (C# 12)
IOrderRepository repo,
ILogger<OrderService> logger)
{
public async Task<OrderDto> GetAsync(Guid id, CancellationToken ct)
{
var order = await repo.GetAsync(id, ct)
?? throw new NotFoundException(id); // ?? throw (C# 7)
return new OrderDto { Id = order.Id, Total = order.Total };
}
}
public record OrderDto // record (C# 9)
{
public required Guid Id { get; init; } // required + init (C# 9, 11)
public required decimal Total { get; init; }
}
Roughly half the lines, and nothing important was lost. The null checks are gone because
nullable reference types plus required cover them at compile time instead.
Interview questions¶
Q: What C# features do you use most from recent versions?
The honest answer is more useful than a long list. Something like: "Primary constructors
from C# 12 removed a lot of noise — nearly every service in an ASP.NET Core app is just
constructor injection, and that used to be six lines of boilerplate per class. Records from
C# 9 replaced most of our DTO classes, since value equality and a readable ToString are
exactly what you want for a data carrier. And nullable reference types changed how we think
about null — turning it on across an existing codebase found several real bugs."
That is three features, each with a reason. It shows use rather than reading.
Q: What is a primary constructor and how does it differ from a record's?
Since C# 12 a class can declare constructor parameters next to its name, and those parameters are usable anywhere in the class body. It removes the field-declare-then-assign boilerplate that every dependency-injected service used to have. The difference from a record is important: a record's primary constructor parameters become public init-only properties with value equality generated around them. A class's primary constructor parameters are just private captured fields — they are not properties, they are not exposed, and they are mutable rather than readonly.
Q: What are collection expressions?
C# 12 introduced [1, 2, 3] as a single syntax that works for arrays, List<T>,
Span<T> and any collection type that opts in. It also brought the spread operator, so
[..first, ..second, extra] combines collections in one expression. The benefit beyond
brevity is that the compiler can pick the most efficient way to build the target type,
rather than you having to know which constructor or method is fastest.
Q: How do global usings and implicit usings work?
A global using declares a namespace import once for the whole project rather than per
file. Implicit usings, controlled by <ImplicitUsings>enable</ImplicitUsings> in the
project file, make the SDK add a standard set automatically based on project type — so a
web project gets the common ASP.NET Core namespaces for free. Together they are why a
modern file often starts directly with the namespace declaration and no using block at all.
Traps ⚠️¶
- Class primary constructor parameters are not properties. They are private, mutable captured fields. Record primary constructor parameters are public init-only properties. Mixing the two up is a common misunderstanding.
- Reciting a changelog sounds worse than naming two features you have used.
- The C# version is tied to the target framework, not chosen freely. You cannot use C# 12 features while targeting .NET 6 without an unsupported override.
- Global usings can cause name collisions that are hard to trace, because the offending
usingis not in the file you are looking at. - Nullable reference types on an existing project produces hundreds of warnings. Enable
it gradually per file with
#nullable enable. - Do not claim experience with a version you have not used. A single follow-up question will expose it, and calibration matters more than coverage.
Related¶
- 1.15 Nullable reference types — the C# 8 headline feature
- 1.2 Records and record structs — C# 9 and 10
- 1.6 required and init — C# 9 and 11
- 1.17 Pattern matching — improved in almost every version
- 4.3 Minimal hosting — built on top-level statements
- VERSIONS.md — the .NET runtime and ASP.NET Core side of the same question