C# 14 Extension Members: Properties, Static Members and More

C# 14's extension blocks add extension properties and static extension members. Here's the syntax, real examples and when to reach for them.

R M Shahidul Islam Shahed · 3 min read

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:

csharp
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:

csharp
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:

csharp
if (title.IsBlank) { /* … */ }        // extension property
var lead = excerpt.Truncate(160);     // extension method

IsBlank 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:

csharp
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);
    }
}
csharp
if (orders.IsEmpty) return Results.NoContent();

Static extension members

Leave out the parameter name and the members become static extensions of the type itself:

csharp
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;
    }
}
csharp
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.IsHtmx on HttpRequest, 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 Async name.
  • Hiding the real API. If string already 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:

csharp
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);
    }
}
csharp
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.

Keep reading

All articles →
C#

C# 14 Features You'll Use Every Day

Extension members, the field keyword, null-conditional assignment and more — with before/after code.

1 min read
C#

Pattern Matching in C#: The Patterns You'll Use Every Week

Type, property, relational, logical and list patterns with switch expressions, shown on real business code instead of shapes and animals.

3 min read
Performance & Optimisation

Measure Before You Optimise: BenchmarkDotNet for .NET Developers

Stopwatch timings lie. Set up BenchmarkDotNet, read its results, measure allocations and avoid the mistakes that make benchmarks meaningless.

4 min read