Skip to content

Commit 490212a

Browse files
committed
final edit pass
1 parent 6dbaa0b commit 490212a

File tree

4 files changed

+87
-209
lines changed

4 files changed

+87
-209
lines changed

docs/csharp/language-reference/compiler-messages/assembly-references.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ f1_keywords:
2020
- "CS7079"
2121
- "CS8090"
2222
- "CS8203"
23+
- "CS9286"
2324
helpviewer_keywords:
2425
- "CS0012"
2526
- "CS0234"
@@ -39,7 +40,8 @@ helpviewer_keywords:
3940
- "CS7079"
4041
- "CS8090"
4142
- "CS8203"
42-
ms.date: 02/28/2024
43+
- "CS9286"
44+
ms.date: 05/27/2025
4345
---
4446
# Resolve errors and warnings related to assembly references
4547

@@ -64,6 +66,7 @@ That's by design. The text closely matches the text of the compiler error / warn
6466
- [**CS7079**](#invalid-assembly-reference): *The type is defined in a module that has not been added. You must add the module.*
6567
- [**CS8090**](#invalid-assembly-reference): *There is an error in a referenced assembly.*
6668
- [**CS8203**](#invalid-assembly-reference): *Invalid assembly name.*
69+
- [**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?)*
6770

6871
In addition, the following warnings are covered in this article:
6972

@@ -76,13 +79,15 @@ The following errors and warnings indicate that you're missing an assembly refer
7679
- **CS0012**: *The type 'type' is defined in an assembly that is not referenced. You must add a reference to assembly 'assembly'.*
7780
- **CS0234**: *The type or namespace name does not exist in the namespace (are you missing an assembly reference?)*
7881
- **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?)*
7983

8084
These compiler errors indicate one of these problems in your code:
8185

8286
- 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).
8387
- You misspelled the name of a type. Check the name of the type.
8488
- 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).
8589
- 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.
8691

8792
## Type forwarding
8893

docs/csharp/language-reference/compiler-messages/extension-declarations.md

Lines changed: 77 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ f1_keywords:
1313
- "CS1110"
1414
- "CS1112"
1515
- "CS1113"
16+
- "CS1743"
1617
- "CS9281"
1718
- "CS9282"
1819
- "CS9283"
@@ -42,6 +43,7 @@ helpviewer_keywords:
4243
- "CS1110"
4344
- "CS1112"
4445
- "CS1113"
46+
- "CS1743"
4547
- "CS9281"
4648
- "CS9282"
4749
- "CS9283"
@@ -65,21 +67,67 @@ helpviewer_keywords:
6567
---
6668
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
6769

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+
70106
- **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.*
73107
- **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.*
74109
- **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.*
77110
- **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.*
80112
- **CS9283**: *Extensions must be declared in a top-level, non-generic, static class.*
81113
- **CS9284**: *The receiver parameter of an extension cannot have a default value.*
82114
- **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.*
83131
- **CS9287**: *A receiver parameter cannot have the same name as an extension container type parameter.*
84132
- **CS9288**: *A parameter, local variable, or local function cannot have the same name as an extension container type parameter.*
85133
- **CS9289**: *Member type parameter has the same name as an extension container type parameter.*
@@ -96,204 +144,32 @@ helpviewer_keywords:
96144
- **CS9304**: *Cannot declare init-only accessors in an extension block.*
97145
- **CS9305**: *Cannot use modifiers on the unnamed receiver parameter of extension block.*
98146

147+
Extension declarations must follow these rules:
99148

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-
static class Test
109-
{
110-
static void ExtMethod(int i, this Test c) // 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-
public static class Extensions
126-
{
127-
public static void Test(ref this int 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:
138-
139-
```csharp
140-
// cs1102.cs
141-
// Compile with: /target:library.
142-
public static class Extensions
143-
{
144-
// No type parameters.
145-
public static void Test(this out int i) {} // CS1102
146-
147-
//Single type parameter
148-
public static void Test<T>(this out T t) {}// CS1102
149-
150-
//Multiple type parameters
151-
public static void Test<T,U,V>(this out U u) {}// CS1102
152-
}
153-
```
154-
155-
## Compiler Error CS1103
156-
157-
The first parameter of an extension method cannot be of type.
149+
- The extension can't include a name token. The extension declares the receiver only.
150+
- The receiver parameter can't have a default value.
158151

159-
The first parameter of an extension method cannot be a pointer type. The following example generates CS1103:
152+
Extension members declared in an extension block must follow these rules, in addition to the [common rules](#common-errors-on-extension-declarations):
160153

161-
```csharp
162-
// cs1103.cs
163-
public static class Extensions
164-
{
165-
public unsafe static char* Test(this char* charP) { return charP; } // CS1103
166-
}
167-
```
154+
- 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.
168158

169-
## Compiler Error CS1105
159+
## Errors related to `this` parameter extension methods
170160

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:
172162

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-
public class Extensions
179-
{
180-
181-
// Single type parameter.
182-
public void Test<T>(this System.String s) {} //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-
public class NonStaticClass // CS1106
196-
{
197-
public static void ExtensionMethod1(this int num) {}
198-
}
199-
200-
public static class StaticGenericClass<T> // CS1106
201-
{
202-
public static void ExtensionMethod2(this int num) {}
203-
}
204-
205-
public static class StaticClass // OK
206-
{
207-
public static void ExtensionMethod3(this int num) {}
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-
public class Test
220-
{
221-
}
222-
static class Out
223-
{
224-
static class Extension
225-
{
226-
static void ExtMethod(this Test c) // 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-
public static class Extensions
244-
{
245-
public static bool Test(this bool b) { return b; }
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:
256-
257-
```csharp
258-
// cs1112.cs
259-
[System.Runtime.CompilerServices.ExtensionAttribute] // CS1112
260-
public class Extensions
261-
{
262-
public bool A(bool b) { return b; }
263-
}
264-
265-
class A { }
266-
```
267-
268-
## Compiler Error CS1113
269-
270-
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-
using System;
279-
public static class Extensions
280-
{
281-
public static S ExtMethod(this S s)
282-
{
283-
return s;
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.*
286168

287-
public struct S
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):
290170

291-
public class Test
292-
{
293-
static int Main()
294-
{
295-
Func<S> f = new S().ExtMethod; // CS1113
296-
return 1;
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

Comments
 (0)