|
1 | 1 | --- |
2 | | -title: "Errors and warnings related to extension declarations" |
| 2 | +title: "Resolve errors and warnings related to extension declarations" |
3 | 3 | description: "These errors and warnings indicate that you need to modify the declaration of an extension method using the `this` modifier on the first parameter, or an extension declaration" |
4 | | -ms.date: 10/16/2025 |
| 4 | +ms.date: 11/07/2025 |
| 5 | +ai-usage: ai-assisted |
5 | 6 | f1_keywords: |
6 | 7 | - "CS1100" |
7 | 8 | - "CS1101" |
@@ -44,6 +45,7 @@ f1_keywords: |
44 | 45 | - "CS9323" |
45 | 46 | - "CS9326" |
46 | 47 | - "CS9329" |
| 48 | + - "CS9339" |
47 | 49 | helpviewer_keywords: |
48 | 50 | - "CS1100" |
49 | 51 | - "CS1101" |
@@ -88,9 +90,15 @@ helpviewer_keywords: |
88 | 90 | - "CS9323" |
89 | 91 | - "CS9326" |
90 | 92 | - "CS9329" |
| 93 | + - "CS9339" |
91 | 94 | --- |
92 | | -# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks |
| 95 | +# Resolve errors and warnings in extension member declarations |
93 | 96 |
|
| 97 | +This article covers the following compiler errors: |
| 98 | + |
| 99 | +<!-- The text in this list generates issues for Acrolinx, because they don't use contractions. |
| 100 | +That's by design. The text closely matches the text of the compiler error / warning for SEO purposes. |
| 101 | + --> |
94 | 102 | - [**CS1100**](#errors-related-to-this-parameter-extension-methods): *Method has a parameter modifier '`this`' which is not on the first parameter* |
95 | 103 | - [**CS1101**](#errors-related-to-this-parameter-extension-methods): *The parameter modifier '`ref`' cannot be used with '`this`'.* |
96 | 104 | - [**CS1102**](#common-errors-on-extension-declarations): *The parameter modifier '`out`' cannot be used with '`this`'.* |
@@ -134,33 +142,33 @@ helpviewer_keywords: |
134 | 142 | - [**CS9323**](#errors-related-to-extension-block-declarations): *Cannot declare instance extension operator for a type that is not known to be a struct and is not known to be a class.* |
135 | 143 | - [**CS9326**](#errors-related-to-extension-block-declarations): *'`name`': extension member names cannot be the same as their extended type.* |
136 | 144 | - [**CS9329**](#errors-related-to-extension-block-declarations): *This extension block collides with another extension block. They result in conflicting content-based type names in metadata.* |
| 145 | +- [**CS9339**](#errors-related-to-extension-block-declarations): *The extension resolution is ambiguous between the following members.* |
137 | 146 |
|
138 | 147 | ## Common errors on extension declarations |
139 | 148 |
|
140 | | -The compiler emits these errors when you violate rules that apply to all extension member declarations, regardless of the syntax chosen: |
141 | | - |
142 | 149 | - **CS1102**: *The parameter modifier '`out`' cannot be used with '`this`'.* |
143 | | -- **CS1106**: *Extension methods must be defined in a non generic static class.* |
144 | 150 | - **CS1103**: *The first parameter of an extension method cannot be of a pointer type.* |
| 151 | +- **CS1106**: *Extension methods must be defined in a non generic static class.* |
145 | 152 | - **CS1109**: *Extension Methods must be defined on top level static classes, 'name' is a nested class.* |
146 | 153 | - **CS1113**: *Extension method defined on a value type cannot be used to create delegates.* |
147 | 154 | - **CS1743**: *Cannot specify a default value for the 'this' parameter.* |
148 | 155 | - **CS9283**: *Extensions must be declared in a top-level, non-generic, static class.* |
149 | 156 | - **CS9284**: *The receiver parameter of an extension cannot have a default value.* |
150 | 157 | - **CS9285**: *An extension container can have only one receiver parameter.* |
151 | 158 |
|
152 | | -Any extension declaration must follow these rules: |
| 159 | +The compiler emits these errors when you violate rules that apply to all extension member declarations, regardless of the syntax chosen. For more information, see [Extension methods](../../programming-guide/classes-and-structs/extension-methods.md). |
153 | 160 |
|
154 | | -- Its containing type (`class` or `struct`) must be non-generic and `static`. |
155 | | -- Its containing type must be a top-level type. It can't be nested in another type. |
156 | | -- Members that extend an instance of a value type can't be converted to delegates. |
157 | | -- The receiver parameter can't include the `out` parameter modifier. |
158 | | -- The receiver parameter can't have a default argument value. |
159 | | -- Pointer types can't be extended. In other words, the parameter you apply the `this` modifier to can't be a pointer type. |
| 161 | +To declare extension members correctly, follow these requirements: |
160 | 162 |
|
161 | | -## Errors related to extension block declarations |
| 163 | +- Declare the containing type as a non-generic `static` class or struct (**CS1106**, **CS9283**). |
| 164 | +- Declare the containing type at the top level, not nested within another type (**CS1109**, **CS9283**). |
| 165 | +- Don't convert extension methods on value types to delegates (**CS1113**). Create a regular method instead. |
| 166 | +- Don't use the `out` parameter modifier on the receiver parameter (**CS1102**). |
| 167 | +- Don't provide default values for the receiver parameter (**CS1743**, **CS9284**). |
| 168 | +- Don't extend pointer types (**CS1103**). The parameter you apply the `this` modifier to can't be a pointer type. |
| 169 | +- Declare only one receiver parameter per extension container (**CS9285**). |
162 | 170 |
|
163 | | -These errors are specific to extension blocks, a C# 14 feature. Extension blocks are declared using the `extension` keyword in a static class. The `extension` declares the type and name of the receiver. All members inside the block declared with `extension` are extension members for that receiver: |
| 171 | +## Errors related to extension block declarations |
164 | 172 |
|
165 | 173 | - **CS9281**: *Extension declarations may not have a name.* |
166 | 174 | - **CS9282**: *Extension declarations can include only methods or properties.* |
@@ -191,52 +199,59 @@ These errors are specific to extension blocks, a C# 14 feature. Extension blocks |
191 | 199 | - **CS9323**: *Cannot declare instance extension operator for a type that is not known to be a struct and is not known to be a class.* |
192 | 200 | - **CS9326**: *'`name`': extension member names cannot be the same as their extended type.* |
193 | 201 | - **CS9329**: *This extension block collides with another extension block. They result in conflicting content-based type names in metadata.* |
| 202 | +- **CS9339**: *The extension resolution is ambiguous between the following members.* |
194 | 203 |
|
195 | | -The contextual keyword [`extension`](../keywords/extension.md) declares an extension block. It can't be used for a type. |
196 | | - |
197 | | -Extension declarations must follow these rules: |
198 | | - |
199 | | -- The extension can't include a name token. The extension declares the receiver only. |
200 | | -- The receiver parameter can't have a default value. |
| 204 | +These errors are specific to extension blocks, a C# 14 feature. Extension blocks are declared using the [`extension`](../keywords/extension.md) contextual keyword in a static class. For more information, see [Extension methods](../../programming-guide/classes-and-structs/extension-methods.md). |
201 | 205 |
|
202 | | -Extension members declared in an extension block must follow these rules, in addition to the [common rules](#common-errors-on-extension-declarations): |
| 206 | +To declare extension blocks correctly, follow these requirements: |
203 | 207 |
|
204 | | -- Only methods and properties are valid extension member types. Extension members can extend an instance, or a type. |
205 | | -- The extension must provide a parameter name for the receiver in order to contain members that extend an instance. |
206 | | -- The receiver parameter name must be unique in that extension block. |
207 | | -- All extension members must use all type parameters declared on the extension. They can add more type parameters. |
208 | | -- Extension blocks can't be nested within another extension block. |
| 208 | +- Don't include a name token in the extension declaration (**CS9281**). The extension declares the receiver only. |
| 209 | +- Don't provide default values for the receiver parameter (**CS9284**, covered in [common errors](#common-errors-on-extension-declarations)). |
| 210 | +- Don't use the `extension` keyword for types or aliases (**CS9306**). It's a contextual keyword for extension blocks only. |
209 | 211 |
|
210 | | -**CS9316** is emitted when you attempt to use an extension member as an argument to the `nameof` operator. Extension members aren't allowed in this context. |
| 212 | +To declare extension members in extension blocks correctly, follow these requirements in addition to the [common rules](#common-errors-on-extension-declarations): |
211 | 213 |
|
212 | | -**CS9317**, **CS9318**, **CS9319**, **CS9320**, **CS9321**, **CS9322**, and **CS9323** are operator-related errors in extension blocks: |
| 214 | +- Include only methods or properties as extension members (**CS9282**). Other member types aren't supported. |
| 215 | +- Provide a parameter name for the receiver to contain instance extension members (**CS9303**). |
| 216 | +- Ensure the receiver parameter name is unique within the extension block and doesn't conflict with type parameters (**CS9287**, **CS9288**, **CS9289**, **CS9290**, **CS9291**, **CS9292**, **CS9294**). |
| 217 | +- Reference all type parameters declared on the extension in the extended type (**CS9295**). Additional type parameters can be added on individual members. |
| 218 | +- Don't nest extension blocks within other extension blocks (**CS9309**). |
| 219 | +- Use the `ref` modifier on the receiver parameter only with value types or generic types constrained to struct (**CS9300**). |
| 220 | +- Use the `in` or `ref readonly` modifier on the receiver parameter only with concrete (non-generic) value types (**CS9301**). |
| 221 | +- Don't use modifiers on unnamed receiver parameters (**CS9305**). |
| 222 | +- Don't declare `protected` members in extension blocks (**CS9302**). Extension members must be accessible where the extension is in scope. |
| 223 | +- Don't declare `init`-only accessors in extension blocks (**CS9304**). Use regular property setters instead. |
| 224 | +- Don't use extension members as arguments to the `nameof` operator (**CS9316**). |
| 225 | +- Choose member names that differ from the extended type name (**CS9326**). |
| 226 | +- Ensure extension blocks have unique content-based type names in metadata (**CS9329**). Consolidate or differentiate extension blocks to avoid conflicts. |
| 227 | +- Resolve ambiguous extension member calls by providing more specific type information or using qualified names (**CS9339**). |
213 | 228 |
|
214 | | -- **CS9317**: Unary operators must have the extended type as their parameter. |
215 | | -- **CS9318**: Increment (`++`) and decrement (`--`) operators must have the extended type as their parameter. |
216 | | -- **CS9319**: Binary operators must have at least one parameter that is the extended type. |
217 | | -- **CS9320**: Shift operators must have the extended type as their first operand. |
218 | | -- **CS9321**: You can't declare user-defined operators in extension blocks that extend static classes. |
219 | | -- **CS9322**: When extending a struct with instance operators, the receiver parameter must use the `ref` modifier. |
220 | | -- **CS9323**: You can't declare instance operators for types that aren't constrained to be either a struct or a class. |
| 229 | +### Extension block operator requirements |
221 | 230 |
|
222 | | -**CS9326** is emitted when an extension member has the same name as the extended type. Choose a different name for the member. |
| 231 | +Extension blocks support user-defined operators with specific requirements: |
223 | 232 |
|
224 | | -**CS9329** occurs when two extension blocks result in conflicting content-based type names in the compiled metadata. This typically happens when multiple extension blocks with the same receiver type and similar characteristics are declared. Consolidate the extension blocks or differentiate them in a way that produces unique metadata names. |
| 233 | +- Unary operators must have the extended type as their parameter (**CS9317**). |
| 234 | +- Increment (`++`) and decrement (`--`) operators must have the extended type as their parameter (**CS9318**). |
| 235 | +- Binary operators must have at least one parameter that is the extended type (**CS9319**). |
| 236 | +- Shift operators must have the extended type as their first operand (**CS9320**). |
| 237 | +- Don't declare user-defined operators in extension blocks that extend static classes (**CS9321**). |
| 238 | +- When extending a struct with instance operators, use the `ref` modifier on the receiver parameter (**CS9322**). |
| 239 | +- Don't declare instance operators for types that aren't constrained to be either a struct or a class (**CS9323**). |
225 | 240 |
|
226 | 241 | ## Errors related to `this` parameter extension methods |
227 | 242 |
|
228 | | -These errors are specific to extension methods where you declare the receiver by adding the `this` modifier to the first parameter of the method: |
229 | | - |
230 | 243 | - **CS1100**: *Method has a parameter modifier '`this`' which is not on the first parameter* |
231 | 244 | - **CS1101**: *The parameter modifier '`ref`' cannot be used with '`this`'.* |
232 | 245 | - **CS1105**: *Extension methods must be static.* |
233 | 246 | - **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?* |
234 | 247 | - **CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.* |
235 | 248 |
|
236 | | -An extension method where the receiver instance includes the `this` modifier must follow these rules, in addition to the [common rules](#common-errors-on-extension-declarations): |
| 249 | +These errors are specific to extension methods where you declare the receiver by adding the `this` modifier to the first parameter. For more information, see [Extension methods](../../programming-guide/classes-and-structs/extension-methods.md). |
| 250 | + |
| 251 | +To declare `this` parameter extension methods correctly, follow these requirements in addition to the [common rules](#common-errors-on-extension-declarations): |
237 | 252 |
|
238 | | -- The method must have the `static` modifier. |
239 | | -- The `this` parameter modifier must be applied to the first parameter. It can't be applied to any other parameters on the method. |
240 | | -- The `ref` `out` parameter modifier can't be applied to the first parameter. To apply `ref`, you need to convert to an extension block. |
241 | | -- In .NET Framework apps, `System.Core.dll` must be added as a reference. |
242 | | -- You must specify the `this` modifier on the first parameter. You can't directly use the <xref:System.Runtime.CompilerServices.ExtensionAttribute> attribute instead. |
| 253 | +- Add the `static` modifier to the method (**CS1105**). |
| 254 | +- Apply the `this` parameter modifier only to the first parameter (**CS1100**). |
| 255 | +- Don't combine the `ref` modifier with the `this` modifier (**CS1101**). To use `ref`, convert to an extension block. |
| 256 | +- Add a reference to `System.Core.dll` in .NET Framework apps (**CS1110**). |
| 257 | +- Use the `this` modifier on the first parameter instead of directly applying the <xref:System.Runtime.CompilerServices.ExtensionAttribute> attribute (**CS1112**). |
0 commit comments