Skip to content

Commit 3edb035

Browse files
authored
Add an overview of extension members in the What's new in C# 14 article (#45859)
* Add an overview of extension members Add an overview of the extension member feature in the What's new in C# 14 article. * proofread
1 parent 449b881 commit 3edb035

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

docs/csharp/whats-new/csharp-14.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,38 @@ You can find any breaking changes introduced in C# 14 in our article on [breakin
2828

2929
## Extension members
3030

31-
You can learn more details by reading the [feature specification](~/_csharplang/proposals/extensions.md) for the new extension members feature.
31+
C# 14 adds new syntax to define *extension members*. The new syntax enables you to declare *extension properties* in addition to extension methods. You can also declare extension members that extend the type, rather than an instance of the type. In other words, these new extension members can appear as static members of the type you extend. The following code example shows an example of the different kinds of extension members you can declare:
32+
33+
```csharp
34+
public static class Enumerable
35+
{
36+
// Extension block
37+
extension<TSource>(IEnumerable<TSource> source) // extension members for IEnumerable<TSource>
38+
{
39+
// Extension property:
40+
public bool IsEmpty => source.Any() == false;
41+
// Extension indexer:
42+
public int this[int index] => source.Skip(index).First();
43+
44+
// Extension method:
45+
public IEnumerable<TSource> Where(Func<TSource, bool> predicate) { ... }
46+
}
47+
48+
// extension block, with a receiver type only
49+
extension<TSource>(IEnumerable<TSource>) // static extension members for IEnumerable<Source>
50+
{
51+
// static extension method:
52+
public static IEnumerable<TSource> Combine(IEnumerable<TSource> first, IEnumerable<TSource> second) { ... }
53+
54+
// static extension property:
55+
public static IEnumerable<TSource> Identity => yield return default;
56+
}
57+
}
58+
```
59+
60+
The members in the first extension block are called as though they're instance members of `IEnumerable<TSource>`, for example `sequence.IsEmpty`. The members in the second extension block are called as though they're static members of `IEnumerable<TSource>`, for example `IEnumerable<int>.Identity`.
61+
62+
You can learn more details by reading the article on [extension members](../programming-guide/classes-and-structs/extension-methods.md) in the programming guide, the language reference article on the [`extension` keyword](../language-reference/keywords/extension.md), and the [feature specification](~/_csharplang/proposals/extensions.md) for the new extension members feature.
3263

3364
## The `field` keyword
3465

@@ -124,7 +155,7 @@ You can simplify the preceding code using the `?.` operator:
124155
customer?.Order = GetCurrentOrder();
125156
```
126157

127-
The right side of the `=` operator is evaluated only when the left side is not null. If `customer` is null, the code doesn't call `GetCurrentOrder`.
158+
The right side of the `=` operator is evaluated only when the left side isn't null. If `customer` is null, the code doesn't call `GetCurrentOrder`.
128159

129160
In addition to assignment, you can use null conditional member access operators with compound assignment operators (`+=`, `-=`, and others). However, increment and decrement, `++` and `--`, aren't allowed.
130161

0 commit comments

Comments
 (0)