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
* Phase 1: move internal error
This diagnostics incidates you found a bug in the compiler. Good job.
* First pass
This assumes a search won't uncover related errors for build configs.
* Move command line diagnostics
I don't want the index.md to be a catch-all, so create a speciic file for command line options and build config issues.
This created more opportunities for consolidation.
* consolidate existing diagnostics
Several existing diagnostics were consolidated around the theme of invalid build configurations.
* consolidate around themes.
Consolidate around themes for this area of diagnostics.
* Update invalid-build-command-line.md
proofread
* build warnings
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,7 @@ f1_keywords:
44
44
- "CS8962"
45
45
- "CS8963"
46
46
- "CS8968"
47
+
- "CS8783"
47
48
- "CS8970"
48
49
- "CS9331"
49
50
helpviewer_keywords:
@@ -89,9 +90,10 @@ helpviewer_keywords:
89
90
- "CS8962"
90
91
- "CS8963"
91
92
- "CS8968"
93
+
- "CS8783"
92
94
- "CS8970"
93
95
- "CS9331"
94
-
ms.date: 02/13/2026
96
+
ms.date: 05/19/2026
95
97
ai-usage: ai-assisted
96
98
---
97
99
# Resolve errors and warnings related to attribute declarations or attribute use in your code
@@ -143,6 +145,7 @@ That's by design. The text closely matches the text of the compiler error / warn
143
145
-[**CS8962**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is overridden by the CallerMemberNameAttribute.*
144
146
-[**CS8963**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is applied with an invalid parameter name.*
145
147
-[**CS8968**](#attribute-arguments-and-parameters): *An attribute type argument cannot use type parameters*
148
+
-[**CS8783**](#conditional-attribute-usage): *Local function 'method' must be 'static' in order to use the Conditional attribute*
146
149
-[**CS8970**](#attribute-arguments-and-parameters): *Type cannot be used in this context because it cannot be represented in metadata.*
147
150
-[**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.*
148
151
@@ -234,16 +237,16 @@ The following errors occur when you use specific predefined .NET attributes inco
234
237
235
238
To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md), [Structure types](../builtin-types/struct.md), <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute>, and [Platform Invoke (P/Invoke)](../../../standard/native-interop/pinvoke.md).
236
239
237
-
-The <xref:System.Runtime.CompilerServices.IndexerNameAttribute>can only be applied to indexers that aren't explicit interface member declarations (**CS0415**). Remove the attribute from explicit interface indexers, because the interface already defines the indexer name.
240
+
-You can apply <xref:System.Runtime.CompilerServices.IndexerNameAttribute> only to indexers that aren't explicit interface member declarations (**CS0415**). Remove the attribute from explicit interface indexers, because the interface already defines the indexer name.
238
241
- You can't apply `IndexerName` to indexers marked with `override` because override indexers inherit their name from the base class (**CS0609**). Remove the `IndexerName` attribute from the override indexer.
239
242
- Every instance field in a type marked with `StructLayout(LayoutKind.Explicit)` must have a <xref:System.Runtime.InteropServices.FieldOffsetAttribute> (**CS0625**). Explicit layout requires that you specify the byte offset for each instance field.
240
-
-The <xref:System.Runtime.InteropServices.FieldOffsetAttribute>can only be placed on members of types that have <xref:System.Runtime.InteropServices.StructLayoutAttribute> set to `LayoutKind.Explicit` (**CS0636**). Add the `StructLayout` attribute to the containing type declaration.
243
+
-You can place the <xref:System.Runtime.InteropServices.FieldOffsetAttribute> only on members of types that have <xref:System.Runtime.InteropServices.StructLayoutAttribute> set to `LayoutKind.Explicit` (**CS0636**). Add the `StructLayout` attribute to the containing type declaration.
241
244
- The `FieldOffset` attribute isn't allowed on `static` or `const` fields because explicit layout applies only to instance fields (**CS0637**). Remove the `FieldOffset` attribute from the static or const field.
242
245
- You can't apply <xref:System.Reflection.DefaultMemberAttribute> to a type that already contains an indexer because the compiler automatically defines the default member for types with indexers (**CS0646**). Remove the `DefaultMember` attribute.
243
246
- All <xref:System.Runtime.CompilerServices.IndexerNameAttribute> attributes within a type must specify the same name (**CS0668**). Change the names to match, because the runtime uses a single name for all indexers on a type.
244
-
-The type specified as an argument for <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute>must be a non-generic, non-nested, non-pointer, non-array type (**CS0735**). Only top-level named types are valid forwarding targets.
247
+
-You must specify a non-generic, non-nested, non-pointer, non-array type as an argument for <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute> (**CS0735**). Only top-level named types are valid forwarding targets.
245
248
- An assembly can have only one <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute> for each external type (**CS0739**). Locate and remove the duplicate `TypeForwardedTo` declaration.
246
-
-The <xref:System.Runtime.CompilerServices.RequiredAttributeAttribute> isn't permitted on types defined in C# (**CS1608**). This attribute is reserved for other languages that need to force compilers to require a particular feature.
249
+
-You can't use the <xref:System.Runtime.CompilerServices.RequiredAttributeAttribute> on types defined in C# (**CS1608**). This attribute is reserved for other languages that need to force compilers to require a particular feature.
247
250
- Some attributes are reserved for the compiler and can't be applied manually in source code (**CS9331**). Replace the attribute with the equivalent C# language syntax that causes the compiler to generate it.
248
251
249
252
## Conditional attribute usage
@@ -258,6 +261,7 @@ You see the following errors when you apply the <xref:System.Diagnostics.Conditi
258
261
-**CS0685**: *Conditional member 'member' cannot have an out parameter.*
259
262
-**CS1618**: *Cannot create delegate with method because it or a method it overrides has a Conditional attribute*
260
263
-**CS1689**: *Attribute is only valid on methods or attribute classes*
264
+
-**CS8783**: *Local function 'method' must be 'static' in order to use the Conditional attribute*
261
265
262
266
To correct these errors, follow these rules. For more information, see <xref:System.Diagnostics.ConditionalAttribute>, [Conditional methods](~/_csharpstandard/standard/attributes.md#23532-conditional-methods), and [Attributes](../../advanced-topics/reflection-and-attributes/index.md).
263
267
@@ -269,6 +273,7 @@ To correct these errors, follow these rules. For more information, see <xref:Sys
269
273
- Conditional methods can't have `out` parameters because the `out` variable value would be undefined when the compiler omits the method call (**CS0685**). Remove the `out` parameters from the method, or remove the `Conditional` attribute.
270
274
- You can't create a delegate that references a conditional method because the method might not exist in builds where the condition symbol isn't defined (**CS1618**). Remove the `Conditional` attribute from the method, or don't use it as a delegate target.
271
275
- The `Conditional` attribute is only valid on methods and attribute classes (**CS1689**). It isn't valid on other declaration types such as non-attribute classes, structs, or interfaces.
276
+
- A local function must be declared `static` to use the `Conditional` attribute (**CS8783**). Non-static local functions capture state from the enclosing method, and the compiler can't safely omit a call that might involve captured variables. Add the `static` modifier to the local function, or remove the `[Conditional]` attribute.
-**CS8358**: *Cannot use attribute constructor because it has 'in' or 'ref readonly' parameters.*
214
217
-**CS8091**: *A constructor cannot be extern and have a constructor initializer.*
218
+
-**CS8760**: *'event': extern event cannot have initializer.*
215
219
216
220
You can declare constructors only in `class` and `struct` types, including `record class` and `record struct` types. For more information, see [Instance constructors](../../programming-guide/classes-and-structs/instance-constructors.md).
217
221
@@ -221,6 +225,7 @@ To fix these errors, try the following suggestions:
221
225
- Remove instance constructors from static classes, because static classes can't be instantiated and therefore can't have instance constructors (**CS0710**). If you need initialization logic, use a static constructor instead.
222
226
- Change `in` or `ref readonly` parameters to pass-by-value parameters in attribute constructors, because attribute constructors don't support `in` or `ref readonly` parameter modifiers (**CS8358**). The runtime instantiates attributes by using reflection, which doesn't support the `in` or `ref readonly` modifier.
223
227
- Remove the `: base()` or `: this()` constructor initializer from an `extern` constructor, because extern constructors can't chain to other constructors (**CS8091**). The implementation of an extern constructor is provided externally, so constructor chaining isn't possible.
228
+
- Remove the initializer from an `extern` event declaration (**CS8760**). Extern events have their add and remove accessors provided by external code, so a field initializer in the source has no effect and isn't permitted by the compiler.
224
229
225
230
The following warning can be generated for constructor declarations:
0 commit comments