Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Extension members in C# 14

Reference for C# 14 extension members: extension blocks with receivers, extension properties, static extensions, generic blocks, and how they compare with this-parameter methods.

C# 14 (with .NET 10) lets you group extensions in an extension block. You declare the receiver once, extension(Receiver name) { ... }, and the members inside can use it. Besides methods, a block can hold extension properties, and with a receiver that has only a type (extension(string)) it can add static members that you call on the type itself.

The old this parameter form keeps working in the same class. Blocks are for the cases it could not express, and for avoiding the repeated this T x and generic parameter lists on many related methods.

C#
static class ListExtensions
{
    extension<T>(List<T> list)
    {
        public bool IsEmpty => list.Count == 0;
    }
}

Try it

C# Extension properties and methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Static extension members and the old syntax side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static class Name
{
    extension(ReceiverType name)                 // instance members use "name"
    {
        public R Method(P p) => ...;
        public T Prop => ...;
    }

    extension(ReceiverType)                      // no name: static members only
    {
        public static R Make() => ...;
    }

    extension<T>(IEnumerable<T> source) where T : notnull
    {
        public bool IsEmpty => !source.Any();
    }
}
Part Description
extension(Type name) Declares the receiver for the block. Members with no static modifier are instance-style and can use name.
extension(Type) A receiver with no name is for static members only.
Generics Type parameters and constraints go on the block, and are shared by its members.
Receiver modifiers ref, ref readonly or in may be applied to a struct receiver in the parentheses.
Allowed members Methods, properties (get/set), indexers, operators, and static versions of these. Not instance fields.

Errors: a field in a block gives CS9282 ("This member is not allowed in an extension block"). As with extension methods, the host class must be a non-nested, non-generic static class and its namespace must be imported, or the member is not found (CS1061).

Usage notes

  • Same priority rules. An instance member of the real type always wins over an extension member of the same name, and extension properties cannot override or hide a real property.
  • No state. An extension property is computed on every access from the receiver. It cannot store anything on the object, and it cannot be assigned unless you write a set that forwards to real members.
  • Binary compatibility. The compiler emits ordinary static methods for the members, so existing callers of classic extension methods are unaffected.
  • Static extensions make helpers read like the type's own API (int.Zero, Point.FromPair(...)), but they are resolved at compile time and cannot be virtual.
  • Requires C# 14 and .NET 10 tooling. Older compilers report syntax errors for extension(. Library authors who must support them keep this methods.
  • When to use blocks: when you want an extension property, a static extension, or many members for one receiver. Single methods are fine in the old form.
  • Nullability. Annotate receivers as you would parameters: extension(string? text) allows null receivers.