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/compiler-messages/expression-tree-restrictions.md
+8-1Lines changed: 8 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,8 @@ f1_keywords:
47
47
- "CS9170"
48
48
- "CS9175"
49
49
- "CS9226"
50
+
- "CS9296"
51
+
- "CS9307"
50
52
helpviewer_keywords:
51
53
- "CS0765"
52
54
- "CS0831"
@@ -94,7 +96,9 @@ helpviewer_keywords:
94
96
- "CS9170"
95
97
- "CS9175"
96
98
- "CS9226"
97
-
ms.date: 09/06/2023
99
+
- "CS9296"
100
+
- "CS9307"
101
+
ms.date: 05/27/2025
98
102
---
99
103
# Resolve errors and warnings generated from expressions prohibited in expression trees
100
104
@@ -149,6 +153,8 @@ That's by design. The text closely matches the text of the compiler error / warn
149
153
-**CS9170** - *An expression tree may not contain an inline array access or conversion.*
150
154
-**CS9175** - *An expression tree may not contain a collection expression.*
151
155
-**CS9226** - *An expression tree may not contain an expanded form of non-array params collection parameter.*
156
+
-**CS9296** - *An expression tree may not contain an extension property access*.
157
+
-**CS9307** - *An expression tree may not contain a named argument specification out of position*.
152
158
153
159
## Expression tree restrictions
154
160
@@ -184,6 +190,7 @@ The following expressions are prohibited:
184
190
185
191
Other restrictions are:
186
192
193
+
- Extension properties can't be accessed as extensions.
187
194
- Attributes can't be applied to the lambda expression, its parameters or return.
188
195
- The lambda expression must be convertible to a type derived from <xref:System.Linq.Expressions.Expression?displayProperty=fullName> whose type parameter is a delegate type.
189
196
-[named and optional parameters](../../programming-guide/classes-and-structs/named-and-optional-arguments.md) are restricted. The expression can't call a method specifying named arguments, and it can't use the default value of an optional parameter.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/extension-declarations.md
+91-19Lines changed: 91 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,24 @@ f1_keywords:
13
13
- "CS1110"
14
14
- "CS1112"
15
15
- "CS1113"
16
+
- "CS9281"
17
+
- "CS9282"
18
+
- "CS9283"
19
+
- "CS9284"
20
+
- "CS9285"
21
+
- "CS9287"
22
+
- "CS9288"
23
+
- "CS9289"
24
+
- "CS9290"
25
+
- "CS9292"
26
+
- "CS9293"
27
+
- "CS9295"
28
+
- "CS9300"
29
+
- "CS9301"
30
+
- "CS9302"
31
+
- "CS9303"
32
+
- "CS9304"
33
+
- "CS9305"
16
34
helpviewer_keywords:
17
35
- "CS1100"
18
36
- "CS1101"
@@ -24,31 +42,66 @@ helpviewer_keywords:
24
42
- "CS1110"
25
43
- "CS1112"
26
44
- "CS1113"
45
+
- "CS9281"
46
+
- "CS9282"
47
+
- "CS9283"
48
+
- "CS9284"
49
+
- "CS9285"
50
+
- "CS9287"
51
+
- "CS9288"
52
+
- "CS9289"
53
+
- "CS9290"
54
+
- "CS9291"
55
+
- "CS9292"
56
+
- "CS9293"
57
+
- "CS9294"
58
+
- "CS9295"
59
+
- "CS9300"
60
+
- "CS9301"
61
+
- "CS9302"
62
+
- "CS9303"
63
+
- "CS9304"
64
+
- "CS9305"
27
65
---
28
66
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
29
67
68
+
-**CS1100**: *Method has a parameter modifier '`this`' which is not on the first parameter*
69
+
-**CS1101**: *The parameter modifier '`ref`' cannot be used with '`this`'.*
70
+
-**CS1102**: *The parameter modifier '`out`' cannot be used with '`this`'.*
71
+
-**CS1103**: *The first parameter of an extension method cannot be of a pointer type.*
72
+
-**CS1105**: *Extension methods must be static.*
73
+
-**CS1106**: *Extension methods must be defined in a non generic static class.*
74
+
-**CS1109**: *Extension Methods must be defined on top level static classes, 'name' is a nested class.*
75
+
-**CS1110**: *Cannot define a new extension because the compiler required type <xref:System.Runtime.CompilerServices.ExtensionAttribute> cannot be found. Are you missing a reference to System.Core.dll?*
30
76
-**CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
77
+
-**CS1113**: *Extension method defined on a value type cannot be used to create delegates.*
78
+
-**CS9281**: *Extension declarations may not have a name.*
79
+
-**CS9282**: *Extension declarations can include only methods or properties.*
80
+
-**CS9283**: *Extensions must be declared in a top-level, non-generic, static class.*
81
+
-**CS9284**: *The receiver parameter of an extension cannot have a default value.*
82
+
-**CS9285**: *An extension container can have only one receiver parameter.*
83
+
-**CS9287**: *A receiver parameter cannot have the same name as an extension container type parameter.*
84
+
-**CS9288**: *A parameter, local variable, or local function cannot have the same name as an extension container type parameter.*
85
+
-**CS9289**: *Member type parameter has the same name as an extension container type parameter.*
86
+
-**CS9290**: *A parameter, local variable, or local function cannot have the same name as an extension parameter.*
87
+
-**CS9291**: *'`value`': an automatically-generated parameter name conflicts with an extension parameter name.*
88
+
-**CS9292**: *A type parameter has the same name as an extension parameter.*
89
+
-**CS9293**: *Cannot use an extension parameter in this context.*
90
+
-**CS9294**: *'`value`': an automatically-generated parameter name conflicts with an extension type parameter name.*
91
+
-**CS9295**: *The extended type must reference all the type parameters declared by the extension, but a type parameter is not referenced.*
92
+
-**CS9300**: *The '`ref`' receiver parameter of an extension block must be a value type or a generic type constrained to struct.*
93
+
-**CS9301**: *The '`in`' or '`ref readonly`' receiver parameter of extension must be a concrete (non-generic) value type.*
94
+
-**CS9302**: *new protected member declared in an extension block.*
95
+
-**CS9303**: *Cannot declare instance members in an extension block with an unnamed receiver parameter.*
96
+
-**CS9304**: *Cannot declare init-only accessors in an extension block.*
97
+
-**CS9305**: *Cannot use modifiers on the unnamed receiver parameter of extension block.*
31
98
32
-
## CS1112
33
-
34
-
This error is generated when the <xref:System.Runtime.CompilerServices.ExtensionAttribute> is used on a non-static class that contains extension methods. If this attribute is used on a static class, another error, such as CS0708: "Cannot declare instance members in a static class," might occur.
35
-
36
-
In C#, extension methods must be defined in a static class and the first parameter of the method is modified with the `this` keyword. Do not use the attribute at all in the source code. For more information, see [Extension Methods](../../../programming-guide/classes-and-structs/extension-methods.md). To correct this error remove the attribute and apply the `this` modifier to the first parameter of the method. The following example generates CS1112:
Method 'name' has a parameter modifier 'this' which is not on the first parameter. The `this` modifier is allowed only on the first parameter of a method, which indicates to the compiler that the method is an extension method. To correct this error remove the `this` modifier from all except the first parameter of the method. The following code generates CS1100 because a `this` parameter is modifying the second parameter:
102
+
Method 'name' has a parameter modifier 'this' which is not on the first parameter.
103
+
104
+
The `this` modifier is allowed only on the first parameter of a method, which indicates to the compiler that the method is an extension method. To correct this error remove the `this` modifier from all except the first parameter of the method. The following code generates CS1100 because a `this` parameter is modifying the second parameter:
52
105
53
106
```csharp
54
107
// cs1100.cs
@@ -101,7 +154,7 @@ public static class Extensions
101
154
102
155
## Compiler Error CS1103
103
156
104
-
The first parameter of an extension method cannot be of type 'type'.
157
+
The first parameter of an extension method cannot be of type.
105
158
106
159
The first parameter of an extension method cannot be a pointer type. The following example generates CS1103:
107
160
@@ -179,7 +232,7 @@ static class Out
179
232
180
233
## Compiler Error CS1110
181
234
182
-
Cannot use 'this' modifier on first parameter of method declaration without a reference to System.Core.dll. Add a reference to System.Core.dll or remove 'this' modifier from the method declaration.
235
+
Cannot define a new extension because the compiler required type '{0}' cannot be found. Are you missing a reference to System.Core.dll?
183
236
184
237
Extension methods are supported on version 3.5 and later of .NET Framework. Extension methods generate metadata which marks the method with an attribute. The attribute class is in system.core.dll. To correct this error, as the message states, add a reference to System.Core.dll or remove the `this` modifier from the method declaration. The following example generates CS1110 if the file is not compiled with a reference to System.Core.dll:
185
238
@@ -193,6 +246,25 @@ public static class Extensions
193
246
}
194
247
```
195
248
249
+
## CS1112
250
+
251
+
Do not use 'System.Runtime.CompilerServices.ExtensionAttribute'. Use the 'this' keyword instead.
252
+
253
+
This error is generated when the <xref:System.Runtime.CompilerServices.ExtensionAttribute> is used on a non-static class that contains extension methods. If this attribute is used on a static class, another error, such as CS0708: "Cannot declare instance members in a static class," might occur.
254
+
255
+
In C#, extension methods must be defined in a static class and the first parameter of the method is modified with the `this` keyword. Do not use the attribute at all in the source code. For more information, see [Extension Methods](../../../programming-guide/classes-and-structs/extension-methods.md). To correct this error remove the attribute and apply the `this` modifier to the first parameter of the method. The following example generates CS1112:
0 commit comments