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
-[**CS9286**](#missing-references): *Type does not contain a definition and no accessible extension member for receiver type could be found (are you missing a using directive or an assembly reference?)*
67
70
68
71
In addition, the following warnings are covered in this article:
69
72
@@ -76,13 +79,15 @@ The following errors and warnings indicate that you're missing an assembly refer
76
79
-**CS0012**: *The type 'type' is defined in an assembly that is not referenced. You must add a reference to assembly 'assembly'.*
77
80
-**CS0234**: *The type or namespace name does not exist in the namespace (are you missing an assembly reference?)*
78
81
-**CS0246**: *The type or namespace name could not be found (are you missing a using directive or an assembly reference?)*
82
+
-**CS9286**: *Type does not contain a definition and no accessible extension member for receiver type could be found (are you missing a using directive or an assembly reference?)*
79
83
80
84
These compiler errors indicate one of these problems in your code:
81
85
82
86
- The project doesn't reference the required assembly. To fix this error, [add a reference to the required assembly](../../../standard/assembly/index.md#add-a-reference-to-an-assembly).
83
87
- You misspelled the name of a type. Check the name of the type.
84
88
- You used a variable name where the name of a <xref:System.Type?displayProperty=nameWithType> was expected, such as in the [`typeof` operator](../operators/type-testing-and-cast.md#the-typeof-operator) or the [`is` operator](../operators/type-testing-and-cast.md#the-is-operator).
85
89
- You used the [global scope operator, (`::`)](../operators/namespace-alias-qualifier.md) when the type isn't in the global namespace.
90
+
- You're accessing an extension member and either the namespace isn't specified in a `using` directive, or you're not referencing the assembly that contains the extension.
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
67
69
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
+
-[**CS1100**](#errors-related-to-this-parameter-extension-methods): *Method has a parameter modifier '`this`' which is not on the first parameter*
71
+
-[**CS1101**](#errors-related-to-this-parameter-extension-methods): *The parameter modifier '`ref`' cannot be used with '`this`'.*
72
+
-[**CS1102**](#common-errors-on-extension-declarations): *The parameter modifier '`out`' cannot be used with '`this`'.*
73
+
-[**CS1103**](#common-errors-on-extension-declarations): *The first parameter of an extension method cannot be of a pointer type.*
74
+
-[**CS1105**](#errors-related-to-this-parameter-extension-methods): *Extension methods must be static.*
75
+
-[**CS1106**](#common-errors-on-extension-declarations): *Extension methods must be defined in a non generic static class.*
76
+
-[**CS1109**](#common-errors-on-extension-declarations): *Extension Methods must be defined on top level static classes, 'name' is a nested class.*
77
+
-[**CS1110**](#errors-related-to-this-parameter-extension-methods): *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?*
78
+
-[**CS1112**](#errors-related-to-this-parameter-extension-methods): *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
79
+
-[**CS1113**](#errors-related-to-this-parameter-extension-methods): *Extension method defined on a value type cannot be used to create delegates.*
80
+
-[**CS1743**](#common-errors-on-extension-declarations): *Cannot specify a default value for the 'this' parameter.*
81
+
-[**CS9281**](#errors-related-to-extension-block-declarations): *Extension declarations may not have a name.*
82
+
-[**CS9282**](#errors-related-to-extension-container-declarations): *Extension declarations can include only methods or properties.*
83
+
-[**CS9283**](#common-errors-on-extension-declarations): *Extensions must be declared in a top-level, non-generic, static class.*
84
+
-[**CS9284**](#common-errors-on-extension-declarations): *The receiver parameter of an extension cannot have a default value.*
85
+
-[**CS9285**](#common-errors-on-extension-declarations): *An extension container can have only one receiver parameter.*
86
+
-[**CS9287**](#errors-related-to-extension-container-declarations): *A receiver parameter cannot have the same name as an extension container type parameter.*
87
+
-[**CS9288**](#errors-related-to-extension-container-declarations): *A parameter, local variable, or local function cannot have the same name as an extension container type parameter.*
88
+
-[**CS9289**](#errors-related-to-extension-container-declarations): *Member type parameter has the same name as an extension container type parameter.*
89
+
-[**CS9290**](#errors-related-to-extension-container-declarations): *A parameter, local variable, or local function cannot have the same name as an extension parameter.*
90
+
-[**CS9291**](#errors-related-to-extension-container-declarations): *'`value`': an automatically-generated parameter name conflicts with an extension parameter name.*
91
+
-[**CS9292**](#errors-related-to-extension-container-declarations): *A type parameter has the same name as an extension parameter.*
92
+
-[**CS9293**](#errors-related-to-extension-container-declarations): *Cannot use an extension parameter in this context.*
93
+
-[**CS9294**](#errors-related-to-extension-container-declarations): *'`value`': an automatically-generated parameter name conflicts with an extension type parameter name.*
94
+
-[**CS9295**](#errors-related-to-extension-container-declarations): *The extended type must reference all the type parameters declared by the extension, but a type parameter is not referenced.*
95
+
-[**CS9300**](#errors-related-to-extension-container-declarations): *The '`ref`' receiver parameter of an extension block must be a value type or a generic type constrained to struct.*
96
+
-[**CS9301**](#errors-related-to-extension-container-declarations): *The '`in`' or '`ref readonly`' receiver parameter of extension must be a concrete (non-generic) value type.*
97
+
-[**CS9302**](#errors-related-to-extension-container-declarations): *new protected member declared in an extension block.*
98
+
-[**CS9303**](#errors-related-to-extension-container-declarations): *Cannot declare instance members in an extension block with an unnamed receiver parameter.*
99
+
-[**CS9304**](#errors-related-to-extension-container-declarations): *Cannot declare init-only accessors in an extension block.*
100
+
-[**CS9305**](#errors-related-to-extension-container-declarations): *Cannot use modifiers on the unnamed receiver parameter of extension block.*
101
+
102
+
## Common errors on extension declarations
103
+
104
+
The compiler emits these errors when you violate rules that apply to all extension member declarations, regardless of the syntax chosen:
105
+
70
106
-**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
107
-**CS1106**: *Extension methods must be defined in a non generic static class.*
108
+
-**CS1103**: *The first parameter of an extension method cannot be of a pointer type.*
74
109
-**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?*
76
-
-**CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
77
110
-**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.*
111
+
-**CS1743**: *Cannot specify a default value for the 'this' parameter.*
80
112
-**CS9283**: *Extensions must be declared in a top-level, non-generic, static class.*
81
113
-**CS9284**: *The receiver parameter of an extension cannot have a default value.*
82
114
-**CS9285**: *An extension container can have only one receiver parameter.*
115
+
116
+
Any extension declaration must follow these rules:
117
+
118
+
- Its containing type (`class` or `struct`) must be non-generic and `static`.
119
+
- Its containing type must be a top-level type. It can't be nested in another type.
120
+
- Members that extend an instance of a value type can't be converted to delegates.
121
+
- The receiver parameter can't include the `out` parameter modifier.
122
+
- The receiver parameter can't have a default argument value.
123
+
- Pointer types can't be extended. In other words, the parameter you apply the `this` modifier to can't be a pointer type.
124
+
125
+
## Errors related to extension block declarations
126
+
127
+
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:
128
+
129
+
-**CS9281**: *Extension declarations may not have a name.*
130
+
-**CS9282**: *Extension declarations can include only methods or properties.*
83
131
-**CS9287**: *A receiver parameter cannot have the same name as an extension container type parameter.*
84
132
-**CS9288**: *A parameter, local variable, or local function cannot have the same name as an extension container type parameter.*
85
133
-**CS9289**: *Member type parameter has the same name as an extension container type parameter.*
@@ -96,204 +144,32 @@ helpviewer_keywords:
96
144
-**CS9304**: *Cannot declare init-only accessors in an extension block.*
97
145
-**CS9305**: *Cannot use modifiers on the unnamed receiver parameter of extension block.*
98
146
147
+
Extension declarations must follow these rules:
99
148
100
-
## Compiler Error CS1100
101
-
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:
105
-
106
-
```csharp
107
-
// cs1100.cs
108
-
staticclassTest
109
-
{
110
-
staticvoidExtMethod(inti, thisTestc) // CS1100
111
-
{
112
-
}
113
-
}
114
-
```
115
-
116
-
## Compiler Error CS1101
117
-
118
-
The parameter modifier 'ref' cannot be used with 'this'.
119
-
120
-
When the `this` keyword modifies the first parameter of a static method, it signals to the compiler that the method is an extension method. With C# version 7.1 and below, no other modifiers are needed or allowed on the first parameter of an extension method. Since C# version 7.2, `ref` extension methods are allowed, take a look at [extension methods](../programming-guide/classes-and-structs/extension-methods.md) for more details. The following example generates CS1101:
121
-
122
-
```csharp
123
-
// cs1101.cs
124
-
// Compile with: /target:library
125
-
publicstaticclassExtensions
126
-
{
127
-
publicstaticvoidTest(refthisint i) {} // CS1101
128
-
}
129
-
```
130
-
131
-
## Compiler Error CS1102
132
-
133
-
The parameter modifier 'out' cannot be used with 'this'.
134
-
135
-
When the `this` keyword modifies the first parameter of a static method, it signals to the compiler that the method is an extension method. No other modifiers are needed or allowed on the first parameter of an extension method.
136
-
137
-
To correct this error remove the unauthorized modifiers from the first parameter. The following example generates CS1102:
- Only methods and properties are valid extension member types. Extension members can extend an instance, or a type.
155
+
- The extension must provide a parameter name for the receiver in order to contain members that extend an instance.
156
+
- The receiver parameter name must be unique in that extension block.
157
+
- All extension members must use all type parameters declared on the extension. They can add more type parameters.
168
158
169
-
## Compiler Error CS1105
159
+
## Errors related to `this` parameter extension methods
170
160
171
-
Extension methods must be static.
161
+
These errors are specific to extension methods where you declare the receiver by adding the `this` modifier to the first parameter of the method:
172
162
173
-
Extension methods must be declared as static methods in a non-generic static class. The following example generates CS1105 because `Test` is not static:
174
-
175
-
```csharp
176
-
// cs1105.cs
177
-
// Compile with: /target:library
178
-
publicclassExtensions
179
-
{
180
-
181
-
// Single type parameter.
182
-
publicvoidTest<T>(thisSystem.Strings) {} //CS1105
183
-
184
-
}
185
-
```
186
-
187
-
## Compiler Error CS1106
188
-
189
-
Extension methods must be defined in a non generic static class.
190
-
191
-
Extension methods must be defined as static methods in a non-generic static class. The following example generates CS1106:
192
-
193
-
```csharp
194
-
// CS1106.cs
195
-
publicclassNonStaticClass// CS1106
196
-
{
197
-
publicstaticvoidExtensionMethod1(thisintnum) {}
198
-
}
199
-
200
-
publicstaticclassStaticGenericClass<T> // CS1106
201
-
{
202
-
publicstaticvoidExtensionMethod2(thisintnum) {}
203
-
}
204
-
205
-
publicstaticclassStaticClass// OK
206
-
{
207
-
publicstaticvoidExtensionMethod3(thisintnum) {}
208
-
}
209
-
```
210
-
211
-
## Compiler Error CS1109
212
-
213
-
Extension Methods must be defined on top level static classes, 'name' is a nested class.
214
-
215
-
Extension methods cannot be defined in nested classes. The following example generates CS1109 because the class `Extension` is nested inside the class `Out`:
216
-
217
-
```csharp
218
-
// cs1109.cs
219
-
publicclassTest
220
-
{
221
-
}
222
-
staticclassOut
223
-
{
224
-
staticclassExtension
225
-
{
226
-
staticvoidExtMethod(thisTestc) // CS1109
227
-
{
228
-
}
229
-
}
230
-
}
231
-
```
232
-
233
-
## Compiler Error CS1110
234
-
235
-
Cannot define a new extension because the compiler required type '{0}' cannot be found. Are you missing a reference to System.Core.dll?
236
-
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:
238
-
239
-
```csharp
240
-
// cs1110.cs
241
-
// CS1110
242
-
// Compile with: /target:library
243
-
publicstaticclassExtensions
244
-
{
245
-
publicstaticboolTest(thisboolb) { returnb; }
246
-
}
247
-
```
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:
Extension methods 'name' defined on value type 'name' cannot be used to create delegates.
271
-
272
-
Extension methods that are defined for class types can be used to create delegates. Extension methods that are defined for value types cannot. To correct this error, associate the extension method with a class type or make the method a regular method on the struct.
273
-
274
-
The following example generates CS1113:
275
-
276
-
```csharp
277
-
// cs1113.cs
278
-
usingSystem;
279
-
publicstaticclassExtensions
280
-
{
281
-
publicstaticSExtMethod(thisSs)
282
-
{
283
-
returns;
284
-
}
285
-
}
163
+
-**CS1100**: *Method has a parameter modifier '`this`' which is not on the first parameter*
164
+
-**CS1101**: *The parameter modifier '`ref`' cannot be used with '`this`'.*
165
+
-**CS1105**: *Extension methods must be static.*
166
+
-**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?*
167
+
-**CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
286
168
287
-
publicstructS
288
-
{
289
-
}
169
+
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):
290
170
291
-
publicclassTest
292
-
{
293
-
staticintMain()
294
-
{
295
-
Func<S>f=newS().ExtMethod; // CS1113
296
-
return1;
297
-
}
298
-
}
299
-
```
171
+
- The method must have the `static` modifier.
172
+
- The `this` parameter modifier must be applied to the first parameter. It can't be applied to any other parameters on the method.
173
+
- The `ref``out` parameter modifier can't be applied to the first parameter. To apply `ref`, you need to convert to an extension block.
174
+
- In .NET Framework apps, `System.Core.dll` must be added as a reference.
175
+
- You must specify the `this` modifier on the first parameter. You can't directly use the <xref:System.Runtime.CompilerServices.ExtensionAttribute> attribute instead.
0 commit comments