For years, extension methods were the only thing you could add to a type you don't own. C# 14 (shipping with .NET 10) replaces that limit with extension blocks: one place to declare extension methods, extension properties and static extension members for a type. This post shows the syntax and the cases where it makes code clearer.
The old way still works
Classic extension methods are unchanged and still compile:
public static class StringExtensions
{
public static bool IsBlank(this string? value) => string.IsNullOrWhiteSpace(value);
}You don't need to rewrite anything. Extension blocks are the new option for the cases the this parameter couldn't express.
Extension blocks
An extension block names the receiver once, then lists members that use it:
public static class StringExtensions
{
extension(string value)
{
public bool IsBlank => string.IsNullOrWhiteSpace(value);
public string Truncate(int max) =>
value.Length <= max ? value : value[..max].TrimEnd() + "…";
}
}Call sites read like members of string:
if (title.IsBlank) { /* … */ } // extension property
var lead = excerpt.Truncate(160); // extension methodIsBlank is a property, which wasn't possible before. Things that are really facts about a value (IsBlank, IsWeekend, IsEmpty) no longer need method parentheses.
Generic receivers
Type parameters go on the extension declaration and are available to every member in the block:
public static class EnumerableExtensions
{
extension<T>(IEnumerable<T> source)
{
public bool IsEmpty => !source.Any();
public IEnumerable<T> WhereNotNull() => source.Where(x => x is not null);
}
}if (orders.IsEmpty) return Results.NoContent();Static extension members
Leave out the parameter name and the members become static extensions of the type itself:
public static class DateOnlyExtensions
{
extension(DateOnly)
{
public static DateOnly Today(TimeProvider time) =>
DateOnly.FromDateTime(time.GetUtcNow().UtcDateTime);
}
extension(DateOnly date)
{
public bool IsWeekend => date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday;
}
}var today = DateOnly.Today(timeProvider);
if (today.IsWeekend) { /* … */ }Static extensions are a good home for factory methods that belong to a type you don't own, instead of a loose DateHelpers class nobody can find.
How it compiles
Extension blocks compile to ordinary static methods, the same shape as classic extension methods. That has two practical results:
- There's no runtime cost: an extension property is a static method call.
- Classic and new-style extensions can sit side by side, and callers can't tell them apart. You can migrate one class at a time.
An extension property can't hold state. There's no hidden field behind it; it computes from the receiver every time, like a method.
Good uses
- Facts about a value:
order.IsOverdue,span.IsEmpty,user.IsAdmin. - Read-only views of library types:
request.IsHtmxonHttpRequest, reading a header. - Factory methods for types you don't own, as static extensions.
- Grouping: one extension block per receiver keeps related helpers together.
Uses to avoid
- Expensive work behind a property. A property that runs a database query surprises every reader. Keep that a method with an
Asyncname. - Hiding the real API. If
stringalready has a method with the same meaning, call it. - Business rules on entities. If you own the type, put the member on the type. Extensions are for types you can't change.
A realistic example
Reading a header in ASP.NET Core:
public static class HttpRequestExtensions
{
extension(HttpRequest request)
{
public bool IsHtmx => request.Headers.ContainsKey("HX-Request");
public bool WantsJson =>
request.Headers.Accept.Any(a => a?.Contains("application/json", StringComparison.OrdinalIgnoreCase) == true);
}
}return Request.IsHtmx ? PartialView("_Rows", model) : View(model);Wrapping up
Extension blocks don't replace extension methods; they finish the idea. Use extension properties for facts, static extensions for factories on types you don't own, and keep anything slow or stateful as a method. Your call sites will read like the type had those members all along.