You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/extension.md
+21-8
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: "extension member declarations"
2
+
title: "Extension member declarations"
3
3
description: "Learn the syntax to declare extension members in C#. Extension members enable you to add functionality to types and interfaces in those instances where you don't have the source for the original type. Extensions are often paired with generic interfaces to implement a common set of functionality across all types that implement that interface."
4
4
ms.date: 04/17/2025
5
5
f1_keywords:
@@ -8,13 +8,13 @@ f1_keywords:
8
8
---
9
9
# Extension declaration (C# Reference)
10
10
11
-
Beginning with C# 14, top level, non-generic`static class` declarations can use `extension` containers to declare *extension members*. Extension members are methods or properties and can appear to be instance or static members. Earlier versions of C# enable *extension methods* by adding `this` as a modifier to the first parameter of a static method declared in a top-level, non-generic static class.
11
+
Beginning with C# 14, top level, nongeneric`static class` declarations can use `extension` containers to declare *extension members*. Extension members are methods or properties and can appear to be instance or static members. Earlier versions of C# enable *extension methods* by adding `this` as a modifier to the first parameter of a static method declared in a top-level, nongeneric static class.
12
12
13
13
The `extension` block specifies the type and receiver for extension members. You can declare methods and properties inside the `extension` declaration. The following example declares a single extension block that defines an instance extension method and an instance property.
The `extension` defines the receiver: `sequence`, which is the an `IEnumerable<int>`. The receiver type can be non-generic, an open generic, or a closed generic type. The name `sequence` is in scope in every instance member declared in that extension. The extension method and property both access `sequence`.
17
+
The `extension` defines the receiver: `sequence`, which is an `IEnumerable<int>`. The receiver type can be nongeneric, an open generic, or a closed generic type. The name `sequence` is in scope in every instance member declared in that extension. The extension method and property both access `sequence`.
18
18
19
19
Any of the extension members can be accessed as though they were members of the receiver type:
20
20
@@ -24,7 +24,7 @@ You can declare any number of members in a single container, as long as they sha
@@ -41,10 +41,23 @@ The `Add` method can be called from any other method as though it was a member o
41
41
42
42
Both forms of extension methods generate the same intermediate language (IL). Callers can't make a distinction between them. In fact, you can convert existing extension methods to the new member syntax without a breaking change. The formats are both binary and source compatible.
43
43
44
-
TODO: Add Generic extension members.
45
-
- Type parameter on receiver
46
-
- Type parameter that's not the receiver on a member
47
-
- Type parameter on receiver + additional
44
+
## Generic extension blocks
45
+
46
+
Where you specify the type parameters for an extension member declared in an extension block depends on where that type parameter is required:
47
+
48
+
- You add the type parameter to the `extension` declaration when the type parameter is used in the receiver.
49
+
- You add the type parameter to the member declaration when the type is distinct from any type parameter specified on the receiver.
50
+
- You can't specify the same type parameter in both locations.
51
+
52
+
The following example shows an extension block for `IEnumerable<T>` where two of the extension members require a second type parameter:
The members `Append` and `Prepend` specify the *extra* type parameter for the conversion. None of the members repeat the type parameter for the receiver.
57
+
58
+
The equivalent extension method declarations demonstrate how those type parameters are encoded:
0 commit comments