Skip to content

Commit 871025f

Browse files
BillWagnerCopilotgewarren
authored
Add errors for existing C# 14 themes (#49669)
* Add errors for existing C# 14 themes Add all newly created diagnostics that map to existing consolidated error files. * Update docs/csharp/language-reference/compiler-messages/array-declaration-errors.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Edit pass on array errors. After adding the final C# 14 updates, perform an edit pass to match the current format. * edit pass on extension declarations Add final errors introduced in C# 14 for extensions. Perform an edit pass to match the improved format. * Edit pass and update dates After adding the final diagnostics for C# 14, update this file to match the more recent style. * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
1 parent 04e66f2 commit 871025f

File tree

7 files changed

+331
-175
lines changed

7 files changed

+331
-175
lines changed

docs/csharp/language-reference/compiler-messages/array-declaration-errors.md

Lines changed: 125 additions & 72 deletions
Large diffs are not rendered by default.

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

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
2-
title: "Errors and warnings related to extension declarations"
2+
title: "Resolve errors and warnings related to extension declarations"
33
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
56
f1_keywords:
67
- "CS1100"
78
- "CS1101"
@@ -44,6 +45,7 @@ f1_keywords:
4445
- "CS9323"
4546
- "CS9326"
4647
- "CS9329"
48+
- "CS9339"
4749
helpviewer_keywords:
4850
- "CS1100"
4951
- "CS1101"
@@ -88,9 +90,15 @@ helpviewer_keywords:
8890
- "CS9323"
8991
- "CS9326"
9092
- "CS9329"
93+
- "CS9339"
9194
---
92-
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
95+
# Resolve errors and warnings in extension member declarations
9396

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+
-->
94102
- [**CS1100**](#errors-related-to-this-parameter-extension-methods): *Method has a parameter modifier '`this`' which is not on the first parameter*
95103
- [**CS1101**](#errors-related-to-this-parameter-extension-methods): *The parameter modifier '`ref`' cannot be used with '`this`'.*
96104
- [**CS1102**](#common-errors-on-extension-declarations): *The parameter modifier '`out`' cannot be used with '`this`'.*
@@ -134,33 +142,33 @@ helpviewer_keywords:
134142
- [**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.*
135143
- [**CS9326**](#errors-related-to-extension-block-declarations): *'`name`': extension member names cannot be the same as their extended type.*
136144
- [**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.*
137146

138147
## Common errors on extension declarations
139148

140-
The compiler emits these errors when you violate rules that apply to all extension member declarations, regardless of the syntax chosen:
141-
142149
- **CS1102**: *The parameter modifier '`out`' cannot be used with '`this`'.*
143-
- **CS1106**: *Extension methods must be defined in a non generic static class.*
144150
- **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.*
145152
- **CS1109**: *Extension Methods must be defined on top level static classes, 'name' is a nested class.*
146153
- **CS1113**: *Extension method defined on a value type cannot be used to create delegates.*
147154
- **CS1743**: *Cannot specify a default value for the 'this' parameter.*
148155
- **CS9283**: *Extensions must be declared in a top-level, non-generic, static class.*
149156
- **CS9284**: *The receiver parameter of an extension cannot have a default value.*
150157
- **CS9285**: *An extension container can have only one receiver parameter.*
151158

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).
153160

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

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**).
162170

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
164172

165173
- **CS9281**: *Extension declarations may not have a name.*
166174
- **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
191199
- **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.*
192200
- **CS9326**: *'`name`': extension member names cannot be the same as their extended type.*
193201
- **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.*
194203

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).
201205

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

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.
209211

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):
211213

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**).
213228

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
221230

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

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**).
225240

226241
## Errors related to `this` parameter extension methods
227242

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-
230243
- **CS1100**: *Method has a parameter modifier '`this`' which is not on the first parameter*
231244
- **CS1101**: *The parameter modifier '`ref`' cannot be used with '`this`'.*
232245
- **CS1105**: *Extension methods must be static.*
233246
- **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?*
234247
- **CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
235248

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):
237252

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**).

docs/csharp/language-reference/compiler-messages/feature-version-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ f1_keywords:
5858
- "CS9268"
5959
- "CS9269"
6060
- "CS9271"
61+
- "CS9327"
62+
- "CS9328"
6163
helpviewer_keywords:
6264
- "CS0171"
6365
- "CS0188"
@@ -115,6 +117,8 @@ helpviewer_keywords:
115117
- "CS9268"
116118
- "CS9269"
117119
- "CS9271"
120+
- "CS9327"
121+
- "CS9328"
118122
ms.date: 05/23/2025
119123
---
120124
# Resolve warnings related to language features and versions
@@ -161,6 +165,8 @@ That's be design. The text closely matches the text of the compiler error / warn
161165
- **CS9268**: *Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.*
162166
- **CS9269**: *UnscopedRefAttribute is only valid in C# 11 or later or when targeting net7.0 or later.*
163167
- [**CS9271**](#implementation-specific-attributes): *The type '`Microsoft.CodeAnalysis.EmbeddedAttribute`' must be non-generic, internal, sealed, non-static, have a parameterless constructor, inherit from System.Attribute, and be able to be applied to any type.*
168+
- **CS9327**: *Feature is not available in C# 14.0. Use newer language version.*
169+
- **CS9328**: *Method uses a feature that is not supported by runtime async currently.*
164170

165171
In addition, the following errors and warnings relate to struct initialization changes in recent versions:
166172

0 commit comments

Comments
 (0)