C# Reference ยท Keywords and Statements
Extension blocks
Reference for C# 14 extension blocks: add extension methods, properties and static members to existing types, and see how they differ from this-parameter methods.
An extension block is the C# 14 way to add members to a type you do not own. Inside a static class, you write extension(Receiver name) { ... } and put the members in the block. The receiver in the parentheses plays the role of the this parameter of a classic extension method, and every member in the block can use it.
Because the receiver is declared once, extension blocks can express more than the old this syntax: extension properties, extension indexers and operators, and extension members that are static (called on the type itself, like int.Parse). Classic extension methods still work and can live in the same class.
public static class StringExtensions { extension(string text) { public bool IsBlank => string.IsNullOrWhiteSpace(text); } }
Try it
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.
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
public static class Name { extension(ReceiverType receiverName) { /* instance members */ } extension(ReceiverType) { /* static members */ } extension<T>(Container<T> receiver) { /* generic receiver */ } extension(ref StructType receiver) { /* ref receiver, for structs */ } }
| Part | Description |
|---|---|
ReceiverType receiverName |
The type being extended and the parameter name that the members use for the instance. ref, in and ref readonly modifiers are allowed. |
| Members | Methods, properties, indexers and operators. Declare them public (or internal). Static members use the receiver type without a name. |
| Type parameters | Written on the block, extension<T>(...); members may add their own. |
Errors: an extension block must be declared in a non-generic static class. An extension member cannot access private members of the receiver, and it cannot add instance state (fields, events).
Usage notes
- Extension members are resolved at compile time. A real member of the type always wins over an extension, so an extension named like an existing method is never called.
- A classic
this-parameter extension method and a block-based one compile to compatible metadata. You can migrate gradually: both forms may live in one static class. - Extension properties are computed on demand. They have no storage, so they cannot be assigned unless you give them a setter that changes the receiver (usually a mutable object).
- Static extension members let you write
int.Twice(21)orEnum.X(), which was impossible withthismethods. - Name conflicts between extension blocks in different imported namespaces cause an ambiguity error just like old extension methods: bring in only the
usingdirectives you need. - Extension members on a value-type receiver work on a copy unless you declare the receiver
ref. - This is a C# 14 feature and requires .NET 10 tooling; older compilers reject the
extensionblock syntax.