From 0ef79066f43167ad4c8d0948883066c3f7874bd8 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 10:03:52 -0500 Subject: [PATCH 01/10] create skeleton --- .github/prompts/error-consolidation.md | 4 ++-- .../attribute-usage-errors.md | 24 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 4 ++++ ...n-t-have-specifics-on-this-csharp-error.md | 2 -- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 863202d303297..1c27e6b4d83e1 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -21,7 +21,7 @@ For each source file I specify in this chat, you'll do the following tasks: - Add the contents of the source file to the destination.md file. - Include the source error code in the YML header for f1_keywords and helpviewer_keywords. - - Add an entry with an anchor for the error error code and its corresponding error message. + - Add an entry with an anchor for the error error code and its corresponding error message. The error message should be the verbatim compiler error message. - Add the contents of the source file as a new H2 in the destination file. - Add a redirection for the source file in the file .openpublishing.redirection.csharp.json. Make the destination point to destination file. Place the new entry in sorted order based on 'source_path_from_root'. - Update the TOC file: @@ -46,7 +46,7 @@ To make sure you've found all related errors, we'll check the source. Look in ` I'll give you error codes one by one. For each, I want you to do the following: - Add the new error code to the front matter of the destination file, for both the `f1_keywords` and `helpview_keywords` table. -- Add the new error code and error message to the table at the top of the destination file. +- Add the new error code and error message to the table at the top of the destination file. The error message should be the verbatim text from the compiler - Add the new error code to the list of `displayName` elements in the TOC file entry for the destination file. - Remove the new error code from the front matter in the file `csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-errors.md` file. diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md new file mode 100644 index 0000000000000..12e7d8566935e --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -0,0 +1,24 @@ +--- +title: Resolve errors and warnings that involve attribute declaration and applying attributes in your code. +description: These compiler errors and warnings indicate errors in attribute declarations, or applying attributes in your code. +f1_keywords: + - "CS9331" +helpviewer_keywords: + - "CS9331" +ms.date: 11/11/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings related to attribute declarations or applying attributes in your code + +This article covers the following compiler errors: + + +- [**CS9331**](#reserved-attributes): *Attribute cannot be applied manually.* + +## Reserved attribute + +- **CS9331**: *Attribute cannot be applied manually.* + +- Replace reserved attributes with the equivalent C# syntax. The compiler generates the attribute in the generated code (**CS9331**). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 081e8b3c26050..293e9cd1ce93d 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -466,6 +466,10 @@ items: CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 + - name: Attribute declaration and usage + href: ./compiler-messages/attribute-usage-errors.md + displayName: > + CS9331 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index bc74762b1800a..6f8b8c95ce343 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -542,8 +542,6 @@ f1_keywords: - "CS9096" - "CS9097" # C# 14 errors begin here - - - "CS9331" - "CS9333" - "CS9334" - "CS9338" From 3665031e22bc8d994f67b845b111106c747ea15f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 11:47:42 -0500 Subject: [PATCH 02/10] Finished consolidation of existing errors Finished all the consolidation for the existing documented errors. --- .github/prompts/error-consolidation.md | 6 +- .openpublishing.redirection.csharp.json | 76 +++ .../attribute-usage-errors.md | 607 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 44 +- docs/csharp/misc/cs0243.md | 35 - docs/csharp/misc/cs0404.md | 29 - docs/csharp/misc/cs0415.md | 48 -- docs/csharp/misc/cs0416.md | 37 -- docs/csharp/misc/cs0447.md | 49 -- docs/csharp/misc/cs0577.md | 35 - docs/csharp/misc/cs0578.md | 32 - docs/csharp/misc/cs0582.md | 28 - docs/csharp/misc/cs0609.md | 62 -- docs/csharp/misc/cs0625.md | 38 -- docs/csharp/misc/cs0629.md | 37 -- docs/csharp/misc/cs0636.md | 37 -- docs/csharp/misc/cs0637.md | 33 - docs/csharp/misc/cs0641.md | 36 -- docs/csharp/misc/cs0646.md | 63 -- docs/csharp/misc/cs0653.md | 40 -- docs/csharp/misc/cs0657.md | 25 - docs/csharp/misc/cs0658.md | 30 - docs/csharp/misc/cs0685.md | 33 - 23 files changed, 690 insertions(+), 770 deletions(-) delete mode 100644 docs/csharp/misc/cs0243.md delete mode 100644 docs/csharp/misc/cs0404.md delete mode 100644 docs/csharp/misc/cs0415.md delete mode 100644 docs/csharp/misc/cs0416.md delete mode 100644 docs/csharp/misc/cs0447.md delete mode 100644 docs/csharp/misc/cs0577.md delete mode 100644 docs/csharp/misc/cs0578.md delete mode 100644 docs/csharp/misc/cs0582.md delete mode 100644 docs/csharp/misc/cs0609.md delete mode 100644 docs/csharp/misc/cs0625.md delete mode 100644 docs/csharp/misc/cs0629.md delete mode 100644 docs/csharp/misc/cs0636.md delete mode 100644 docs/csharp/misc/cs0637.md delete mode 100644 docs/csharp/misc/cs0641.md delete mode 100644 docs/csharp/misc/cs0646.md delete mode 100644 docs/csharp/misc/cs0653.md delete mode 100644 docs/csharp/misc/cs0657.md delete mode 100644 docs/csharp/misc/cs0658.md delete mode 100644 docs/csharp/misc/cs0685.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 1c27e6b4d83e1..e3dc05181a9c0 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -12,8 +12,8 @@ Overall steps: We're going to work through a series of files consolidating errors and warnings. -- For the duration of this chat, all references to "destination file" refer to `async-await-errors.md. -- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to `async` function declarations and `await` expressions. +- For the duration of this chat, all references to "destination file" refer to `attribute-usage-errors.md`. +- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to declaring and using *attributes*. The destination file already contains a skeleton for the final output. @@ -31,7 +31,7 @@ For each source file I specify in this chat, you'll do the following tasks: ## Search for other related articles that may be missed. -Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve the target theme. Give me a list to review for possible additional consolidation. Don't make any edits until the originating user approves. +First, let's find candidate source filees. Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve the target theme. Give me a list to review for possible additional consolidation. Don't make any edits until the originating user approves. ## Final search in roslyn source diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 15f9328c66834..a8867a28f0d8f 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1717,6 +1717,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0242.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0243.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0244.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" @@ -1793,6 +1797,22 @@ "source_path_from_root": "/docs/csharp/misc/cs0400.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0404.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0415.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0416.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0447.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0431.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" @@ -1929,6 +1949,26 @@ "source_path_from_root": "/docs/csharp/misc/cs0576.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0577.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0578.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0582.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0609.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0629.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0591.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" @@ -1949,6 +1989,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0623.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0625.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0631.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1957,10 +2001,38 @@ "source_path_from_root": "/docs/csharp/misc/cs0633.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0636.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0637.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0641.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0646.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0643.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0653.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0657.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0658.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0655.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" @@ -1973,6 +2045,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0674.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0685.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/attribute-usage-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0687.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 12e7d8566935e..a67ea306d5502 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -2,8 +2,46 @@ title: Resolve errors and warnings that involve attribute declaration and applying attributes in your code. description: These compiler errors and warnings indicate errors in attribute declarations, or applying attributes in your code. f1_keywords: + - "CS0243" + - "CS0404" + - "CS0415" + - "CS0416" + - "CS0447" + - "CS0577" + - "CS0578" + - "CS0582" + - "CS0609" + - "CS0625" + - "CS0629" + - "CS0636" + - "CS0637" + - "CS0641" + - "CS0646" + - "CS0653" + - "CS0657" + - "CS0658" + - "CS0685" - "CS9331" helpviewer_keywords: + - "CS0243" + - "CS0404" + - "CS0415" + - "CS0416" + - "CS0447" + - "CS0577" + - "CS0578" + - "CS0582" + - "CS0609" + - "CS0625" + - "CS0629" + - "CS0636" + - "CS0637" + - "CS0641" + - "CS0646" + - "CS0653" + - "CS0657" + - "CS0658" + - "CS0685" - "CS9331" ms.date: 11/11/2025 ai-usage: ai-assisted @@ -15,8 +53,577 @@ This article covers the following compiler errors: +- [**CS0243**](#conditional-attribute-on-override): *The Conditional attribute is not valid on 'method' because it is an override method* +- [**CS0404**](#generic-attributes): *'<' unexpected : attributes cannot be generic* +- [**CS0415**](#indexername-attribute): *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* +- [**CS0416**](#attribute-argument-type-parameters): *'type parameter': an attribute argument cannot use type parameters* +- [**CS0447**](#attributes-on-type-arguments): *Attributes cannot be used on type arguments, only on type parameters* +- [**CS0577**](#conditional-attribute-restrictions): *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* +- [**CS0578**](#conditional-attribute-return-type): *The Conditional attribute is not valid on 'function' because its return type is not void* +- [**CS0582**](#conditional-on-interface-members): *The Conditional not valid on interface members* +- [**CS0609**](#indexername-on-override): *Cannot set the IndexerName attribute on an indexer marked override* +- [**CS0625**](#fieldoffset-attribute-required): *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- [**CS0629**](#conditional-on-interface-implementation): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* +- [**CS0636**](#structlayout-required): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* +- [**CS0637**](#fieldoffset-on-static-const): *The FieldOffset attribute is not allowed on static or const fields.* +- [**CS0641**](#attributeusage-attribute): *'attribute' : attribute is only valid on classes derived from System.Attribute* +- [**CS0646**](#defaultmember-attribute): *Cannot specify the DefaultMember attribute on a type containing an indexer* +- [**CS0653**](#abstract-attribute-class): *Cannot apply attribute class 'class' because it is abstract* +- [**CS0657**](#invalid-attribute-location): *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* +- [**CS0658**](#unrecognized-attribute-location): *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* +- [**CS0685**](#conditional-with-out-parameter): *Conditional member 'member' cannot have an out parameter* - [**CS9331**](#reserved-attributes): *Attribute cannot be applied manually.* +## Conditional attribute on override + +- **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method* + +The attribute is not allowed on a method that is marked with the [override](../keywords/override.md) keyword. For more information, see [Knowing When to Use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). + +The compiler never binds to override methods. It only binds to the base method, and the common language runtime calls the override, as appropriate. + +The following code generates CS0243: + +```csharp +// CS0243.cs +// compile with: /target:library +public class MyClass +{ + public virtual void M() {} +} + +public class MyClass2 : MyClass +{ + [System.Diagnostics.ConditionalAttribute("MySymbol")] // CS0243 + // remove Conditional attribute or remove override keyword + public override void M() {} +} +``` + +## Generic attributes + +- **CS0404**: *'<' unexpected : attributes cannot be generic* + +Generic type parameters are not allowed in attributes. Remove the type parameter and angled brackets. + +The following sample generates CS0404: + +```csharp +// CS0404.cs +[MyAttrib] // CS0404 +class C +{ + public static void Main() + { + + } +} +``` + +## IndexerName attribute + +- **CS0415**: *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* + +This error occurs if you use an IndexerName attribute on an indexer that was an explicit implementation of an interface. This error may be avoided by removing the interface name from the declaration of the indexer, if possible. For more information, see the [IndexerNameAttribute Class](xref:System.Runtime.CompilerServices.IndexerNameAttribute). + +The following sample generates CS0415: + +```csharp +// CS0415.cs +using System; +using System.Runtime.CompilerServices; + +public interface IA +{ + int this[int index] + { + get; + set; + } +} + +public class A : IA +{ + [IndexerName("Item")] // CS0415 + int IA.this[int index] + // Try this line instead: + // public int this[int index] + { + get { return 0; } + set { } + } + + static void Main() + { + } +} +``` + +## Attribute argument type parameters + +- **CS0416**: *'type parameter': an attribute argument cannot use type parameters* + +A type parameter was used as an attribute argument, which is not allowed. Use a non-generic type. + +The following sample generates CS0416: + +```csharp +// CS0416.cs +public class MyAttribute : System.Attribute +{ + public MyAttribute(System.Type t) + { + } +} + +class G +{ + + [MyAttribute(typeof(G))] // CS0416 + public void F() + { + } + +} +``` + +## Attributes on type arguments + +- **CS0447**: *Attributes cannot be used on type arguments, only on type parameters* + +This error occurs when you apply an attribute to a type argument that occurs in an invocation statement. It is acceptable to apply an attribute to a type parameter in a class or method declaration statement such as the following: + +```csharp +class C<[some attribute] T> {…} +``` + +The following line of code will generate this error. It is assumed that the class `C`, defined in the previous line of code, has a static method called `MyStaticMethod`. + +```csharp +C<[some attribute] T>.MyStaticMethod(); +``` + +The following code generates error CS0447: + +```csharp +// CS0447.cs +using System; + +namespace Test41 +{ + public interface I + { + void Meth(); + } + public class B : I + { + void I<[Test] int>.Meth() { } // CS0447 + } +} +``` + +> [!NOTE] +> This compiler error is no longer used in Roslyn. + +## Conditional attribute restrictions + +- **CS0577**: *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* + +> [!NOTE] +> Destructor is a deprecated term for [finalizer](../../programming-guide/classes-and-structs/finalizers.md). + +`Conditional` cannot be applied to the specified methods. + +For example, you cannot use some attributes on an explicit interface definition. The following sample generates CS0577: + +```csharp +// CS0577.cs +// compile with: /target:library +interface I +{ + void m(); +} + +public class MyClass : I +{ + [System.Diagnostics.Conditional("a")] // CS0577 + void I.m() {} +} +``` + +## Conditional attribute return type + +- **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void* + + cannot be applied to a method that has a return type other than `void`. The reason for this is that any other return type for a method may be needed by another part of your program. + +The following sample generates CS0578. To resolve this error, you must either delete , or you must change the return value of the method to `void`. + +```csharp +// CS0578.cs +// compile with: /target:library +public class MyClass +{ + [System.Diagnostics.ConditionalAttribute("a")] // CS0578 + public int TestMethod() + { + return 0; + } +} +``` + +## Conditional on interface members + +- **CS0582**: *The Conditional not valid on interface members* + +**ConditionalAttribute** is not valid on an interface member. + +The following sample generates CS0582: + +```csharp +// CS0582.cs +// compile with: /target:library +using System.Diagnostics; +interface MyIFace +{ + [ConditionalAttribute("DEBUG")] // CS0582 + void zz(); +} +``` + +## IndexerName on override + +- **CS0609**: *Cannot set the IndexerName attribute on an indexer marked override* + +The name attribute (**IndexerNameAttribute**) cannot be applied to an indexed property that is an override. For more information, see [Indexers](../../programming-guide/indexers/index.md). + +The following sample generates CS0609: + +```csharp +// CS0609.cs +using System; +using System.Runtime.CompilerServices; + +public class idx +{ + public virtual int this[int iPropIndex] + { + get + { + return 0; + } + set + { + } + } +} + +public class MonthDays : idx +{ + [IndexerName("MonthInfoIndexer")] // CS0609, delete to resolve this CS0609 + public override int this[int iPropIndex] + { + get + { + return 0; + } + set + { + } + } +} + +public class test +{ + public static void Main(string[] args) + { + } +} +``` + +> [!NOTE] +> This compiler error is no longer used in Roslyn. The previous code should compile successfully. + +## FieldOffset attribute required + +- **CS0625**: *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* + +When a struct is marked with an explicit **StructLayout** attribute, all fields in the struct must have the [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute. For more information, see [StructLayoutAttribute Class](xref:System.Runtime.InteropServices.StructLayoutAttribute). + +The following sample generates CS0625: + +```csharp +// CS0625.cs +// compile with: /target:library +using System; +using System.Runtime.InteropServices; + +[StructLayout(LayoutKind.Explicit)] +struct A +{ + public int i; // CS0625 not static; an instance field +} + +// OK +[StructLayout(LayoutKind.Explicit)] +struct B +{ + [FieldOffset(5)] + public int i; +} +``` + +## Conditional on interface implementation + +- **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* + +The attribute cannot be used on the implementation of an interface. + +The following sample generates CS0629: + +```csharp +// CS0629.cs +interface MyInterface +{ + void MyMethod(); +} + +public class MyClass : MyInterface +{ + [System.Diagnostics.Conditional("debug")] + public void MyMethod() // CS0629, remove the Conditional attribute + { + } + + public static void Main() + { + } +} +``` + +## StructLayout required + +- **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* + +You must use the **StructLayout(LayoutKind.Explicit)** attribute on the struct declaration, if it contains any members marked with the **FieldOffset** attribute. For more information, see [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute). + +The following sample generates CS0636: + +```csharp +// CS0636.cs +using System; +using System.Runtime.InteropServices; + +// To resolve the error, uncomment the following line: +// [StructLayout(LayoutKind.Explicit)] +struct Worksheet +{ + [FieldOffset(4)]public int i; // CS0636 +} + +public class MainClass +{ + public static void Main () + { + } +} +``` + +## FieldOffset on static const + +- **CS0637**: *The FieldOffset attribute is not allowed on static or const fields.* + +The [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute cannot be used on fields marked with [static](../keywords/static.md) or [const](../keywords/const.md). + +The following sample generates CS0637: + +```csharp +// CS0637.cs +using System; +using System.Runtime.InteropServices; + +[StructLayout(LayoutKind.Explicit)] +public class MainClass +{ + [FieldOffset(3)] // CS0637 + public static int i; + public static void Main () + { + } +} +``` + +## AttributeUsage attribute + +- **CS0641**: *'attribute' : attribute is only valid on classes derived from System.Attribute* + +An attribute was used that can only be used on a class that derives from **System.Attribute**. + +The following sample generates CS0641: + +```csharp +// CS0641.cs +using System; + +[AttributeUsage(AttributeTargets.All)] +public class NonAttrClass // CS0641 +// try the following line instead +// public class NonAttrClass : Attribute +{ +} + +class MyClass +{ + public static void Main() + { + } +} +``` + +## DefaultMember attribute + +- **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* + +If a class or other type specifies **System.Reflection.DefaultMemberAttribute**, it cannot contain an indexer. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md). + +The following sample generates CS0646: + +```csharp +// CS0646.cs +// compile with: /target:library +[System.Reflection.DefaultMemberAttribute("x")] // CS0646 +class MyClass +{ + public int this[int index] // an indexer + { + get + { + return 0; + } + } + + public int x = 0; +} + +// OK +[System.Reflection.DefaultMemberAttribute("x")] +class MyClass2 +{ + public int prop + { + get + { + return 0; + } + } + + public int x = 0; +} + +class MyClass3 +{ + public int this[int index] // an indexer + { + get + { + return 0; + } + } + + public int x = 0; +} +``` + +## Abstract attribute class + +- **CS0653**: *Cannot apply attribute class 'class' because it is abstract* + +An [abstract](../keywords/abstract.md) custom attribute class cannot be used as an attribute. + +The following sample generates CS0653: + +```csharp +// CS0653.cs +using System; + +public abstract class MyAttribute : Attribute +{ +} + +public class My2Attribute : MyAttribute +{ +} + +[My] // CS0653 +// try the following line instead +// [My2] +class MyClass +{ + public static void Main() + { + } +} +``` + +## Invalid attribute location + +- **CS0657**: *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* + +The compiler found an attribute modifier in an invalid location. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. + +The following sample generates CS0657: + +```csharp +// CS0657.cs +// compile with: /target:library +public class TestAttribute : System.Attribute {} +[return: Test] // CS0657 return not valid on a class +class Class1 {} +``` + +## Unrecognized attribute location + +- **CS0658**: *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* + +An invalid attribute modifier was specified. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. + +The following sample generates CS0658: + +```csharp +// CS0658.cs +using System; +public class TestAttribute : Attribute {} +[badAttributeLocation: Test] // CS0658, badAttributeLocation is invalid +class ClassTest +{ + public static void Main() + { + } +} +``` + +## Conditional with out parameter + +- **CS0685**: *Conditional member 'member' cannot have an out parameter* + +When using the attribute on a method, that method may not have an out parameter. This is because the value of the variable used for the out parameter would not be defined in the case that the method call is compiled to nothing. To avoid this error, remove the out parameter from the conditional method declaration, or don't use the Conditional Attribute. + +The following sample generates CS0685: + +```csharp +// CS0685.cs +using System.Diagnostics; + +class C +{ + [Conditional("DEBUG")] + void trace(out int i) // CS0685 + { + i = 1; + } +} +``` + ## Reserved attribute - **CS9331**: *Attribute cannot be applied manually.* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 293e9cd1ce93d..687326b39dd2d 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -469,7 +469,8 @@ items: - name: Attribute declaration and usage href: ./compiler-messages/attribute-usage-errors.md displayName: > - CS9331 + CS0243, CS0404, CS0415, CS0416, CS0447, CS0577, CS0578, CS0582, CS0609, CS0625, + CS0629, CS0636, CS0637, CS0641, CS0646, CS0653, CS0657, CS0658, CS0685, CS9331 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > @@ -511,7 +512,8 @@ items: href: ./compiler-messages/async-await-errors.md displayName: > async, await, - CS1983, CS1985, CS1986, CS1989, CS1991, CS1992, CS1994, CS1995, CS1996, CS1997, CS1998, CS4008, CS4009, CS4014, CS4032, CS4033, CS8892, CS9123, CS9330 + CS1983, CS1985, CS1986, CS1989, CS1991, CS1992, CS1994, CS1995, CS1996, CS1997, + CS1998, CS4008, CS4009, CS4014, CS4032, CS4033, CS8892, CS9123, CS9330 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > @@ -927,8 +929,6 @@ items: href: ../misc/cs0239.md - name: CS0241 href: ../misc/cs0241.md - - name: CS0243 - href: ../misc/cs0243.md - name: CS0247 href: ../misc/cs0247.md - name: CS0249 @@ -989,8 +989,6 @@ items: href: ../misc/cs0401.md - name: CS0403 href: ../misc/cs0403.md - - name: CS0404 - href: ../misc/cs0404.md - name: CS0405 href: ../misc/cs0405.md - name: CS0406 @@ -1007,10 +1005,6 @@ items: href: ../misc/cs0412.md - name: CS0413 href: ./compiler-messages/cs0413.md - - name: CS0415 - href: ../misc/cs0415.md - - name: CS0416 - href: ../misc/cs0416.md - name: CS0417 href: ./compiler-messages/cs0417.md - name: CS0418 @@ -1045,8 +1039,6 @@ items: href: ./compiler-messages/cs0445.md - name: CS0446 href: ./compiler-messages/cs0446.md - - name: CS0447 - href: ../misc/cs0447.md - name: CS0449 href: ../misc/cs0449.md - name: CS0450 @@ -1165,14 +1157,8 @@ items: href: ../misc/cs0574.md - name: CS0575 href: ../misc/cs0575.md - - name: CS0577 - href: ../misc/cs0577.md - - name: CS0578 - href: ../misc/cs0578.md - name: CS0579 href: ./compiler-messages/cs0579.md - - name: CS0582 - href: ../misc/cs0582.md - name: CS0583 href: ../misc/cs0583.md - name: CS0584 @@ -1195,8 +1181,6 @@ items: href: ../misc/cs0596.md - name: CS0601 href: ../misc/cs0601.md - - name: CS0609 - href: ../misc/cs0609.md - name: CS0610 href: ../misc/cs0610.md - name: CS0616 @@ -1209,30 +1193,16 @@ items: href: ../misc/cs0621.md - name: CS0622 href: ../misc/cs0622.md - - name: CS0625 - href: ../misc/cs0625.md - - name: CS0629 - href: ../misc/cs0629.md - name: CS0635 href: ../misc/cs0635.md - - name: CS0636 - href: ../misc/cs0636.md - - name: CS0637 - href: ../misc/cs0637.md - - name: CS0641 - href: ../misc/cs0641.md - name: CS0644 href: ../misc/cs0644.md - name: CS0645 href: ../misc/cs0645.md - - name: CS0646 - href: ../misc/cs0646.md - name: CS0647 href: ../misc/cs0647.md - name: CS0648 href: ../misc/cs0648.md - - name: CS0653 - href: ../misc/cs0653.md - name: CS0656 href: ../misc/cs0656.md - name: CS0662 @@ -1261,8 +1231,6 @@ items: href: ../misc/cs0682.md - name: CS0683 href: ../misc/cs0683.md - - name: CS0685 - href: ../misc/cs0685.md - name: CS0686 href: ./compiler-messages/cs0686.md - name: CS0689 @@ -1909,10 +1877,6 @@ items: href: ../misc/cs0612.md - name: CS0626 href: ../misc/cs0626.md - - name: CS0657 - href: ../misc/cs0657.md - - name: CS0658 - href: ../misc/cs0658.md - name: CS0672 href: ../misc/cs0672.md - name: CS0684 diff --git a/docs/csharp/misc/cs0243.md b/docs/csharp/misc/cs0243.md deleted file mode 100644 index 334bfcceb35c1..0000000000000 --- a/docs/csharp/misc/cs0243.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0243" -title: "Compiler Error CS0243" -ms.date: 07/20/2015 -f1_keywords: - - "CS0243" -helpviewer_keywords: - - "CS0243" -ms.assetid: 2506e4cb-dc26-46b4-a58c-ab6bf1601144 ---- -# Compiler error CS0243 - -The Conditional attribute is not valid on 'method' because it is an override method - -The attribute is not allowed on a method that is marked with the [override](../language-reference/keywords/override.md) keyword. For more information, see [Knowing When to Use Override and New Keywords](../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). - -The compiler never binds to override methods. It only binds to the base method, and the common language runtime calls the override, as appropriate. - -The following code generates CS0243: - -```csharp -// CS0243.cs -// compile with: /target:library -public class MyClass -{ - public virtual void M() {} -} - -public class MyClass2 : MyClass -{ - [System.Diagnostics.ConditionalAttribute("MySymbol")] // CS0243 - // remove Conditional attribute or remove override keyword - public override void M() {} -} -``` diff --git a/docs/csharp/misc/cs0404.md b/docs/csharp/misc/cs0404.md deleted file mode 100644 index c92a29f46bdfe..0000000000000 --- a/docs/csharp/misc/cs0404.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -description: "Compiler Error CS0404" -title: "Compiler Error CS0404" -ms.date: 07/20/2015 -f1_keywords: - - "CS0404" -helpviewer_keywords: - - "CS0404" -ms.assetid: 60bef49e-e0b7-4e9e-aab3-7afc20a19cb8 ---- -# Compiler Error CS0404 - -'<' unexpected : attributes cannot be generic - - Generic type parameters are not allowed in attributes. Remove the type parameter and angled brackets. - - The following sample generates CS0404: - -```csharp -// CS0404.cs -[MyAttrib] // CS0404 -class C -{ - public static void Main() - { - - } -} -``` diff --git a/docs/csharp/misc/cs0415.md b/docs/csharp/misc/cs0415.md deleted file mode 100644 index df0286bc8871e..0000000000000 --- a/docs/csharp/misc/cs0415.md +++ /dev/null @@ -1,48 +0,0 @@ ---- -description: "Compiler Error CS0415" -title: "Compiler Error CS0415" -ms.date: 07/20/2015 -f1_keywords: - - "CS0415" -helpviewer_keywords: - - "CS0415" -ms.assetid: 1ed45b02-4568-4af4-b2a6-c8b01230d19a ---- -# Compiler Error CS0415 - -The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration - - This error occurs if you use an IndexerName attribute on an indexer that was an explicit implementation of an interface. This error may be avoided by removing the interface name from the declaration of the indexer, if possible. For more information, see the [IndexerNameAttribute Class](xref:System.Runtime.CompilerServices.IndexerNameAttribute). - - The following sample generates CS0415: - -```csharp -// CS0415.cs -using System; -using System.Runtime.CompilerServices; - -public interface IA -{ - int this[int index] - { - get; - set; - } -} - -public class A : IA -{ - [IndexerName("Item")] // CS0415 - int IA.this[int index] - // Try this line instead: - // public int this[int index] - { - get { return 0; } - set { } - } - - static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0416.md b/docs/csharp/misc/cs0416.md deleted file mode 100644 index f2fd0c9d1c787..0000000000000 --- a/docs/csharp/misc/cs0416.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Error CS0416" -title: "Compiler Error CS0416" -ms.date: 07/20/2015 -f1_keywords: - - "CS0416" -helpviewer_keywords: - - "CS0416" -ms.assetid: 61bfe40d-5e87-47e5-933f-3491e28ace42 ---- -# Compiler Error CS0416 - -'type parameter': an attribute argument cannot use type parameters - - A type parameter was used as an attribute argument, which is not allowed. Use a non-generic type. - - The following sample generates CS0416: - -```csharp -// CS0416.cs -public class MyAttribute : System.Attribute -{ - public MyAttribute(System.Type t) - { - } -} - -class G -{ - - [MyAttribute(typeof(G))] // CS0416 - public void F() - { - } - -} -``` diff --git a/docs/csharp/misc/cs0447.md b/docs/csharp/misc/cs0447.md deleted file mode 100644 index 5a8e47c84f282..0000000000000 --- a/docs/csharp/misc/cs0447.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -description: "Compiler Error CS0447" -title: "Compiler Error CS0447" -ms.date: 07/20/2015 -f1_keywords: - - "CS0447" -helpviewer_keywords: - - "CS0447" -ms.assetid: a25486c5-e9bf-4528-8533-c1aaab22ace0 ---- -# Compiler Error CS0447 - -Attributes cannot be used on type arguments, only on type parameters - -This error occurs when you apply an attribute to a type argument that occurs in an invocation statement. It is acceptable to apply an attribute to a type parameter in a class or method declaration statement such as the following: - -```csharp -class C<[some attribute] T> {…} -``` - -The following line of code will generate this error. It is assumed that the class `C`, defined in the previous line of code, has a static method called `MyStaticMethod`. - -```csharp -C<[some attribute] T>.MyStaticMethod(); -``` - -## Example - -The following code generates error CS0447: - -```csharp -// CS0447.cs -using System; - -namespace Test41 -{ - public interface I - { - void Meth(); - } - public class B : I - { - void I<[Test] int>.Meth() { } // CS0447 - } -} -``` - -> [!NOTE] -> This compiler error is no longer used in Roslyn. diff --git a/docs/csharp/misc/cs0577.md b/docs/csharp/misc/cs0577.md deleted file mode 100644 index 099127a50c2ea..0000000000000 --- a/docs/csharp/misc/cs0577.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0577" -title: "Compiler Error CS0577" -ms.date: 07/20/2015 -f1_keywords: - - "CS0577" -helpviewer_keywords: - - "CS0577" -ms.assetid: 34f8f453-f016-4f2c-981a-0d61449cd74b ---- -# Compiler Error CS0577 - -The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation - -> [!NOTE] -> Destructor is a deprecated term for [finalizer](../programming-guide/classes-and-structs/finalizers.md). - - `Conditional` cannot be applied to the specified methods. - - For example, you cannot use some attributes on an explicit interface definition. The following sample generates CS0577: - -```csharp -// CS0577.cs -// compile with: /target:library -interface I -{ - void m(); -} - -public class MyClass : I -{ - [System.Diagnostics.Conditional("a")] // CS0577 - void I.m() {} -} -``` diff --git a/docs/csharp/misc/cs0578.md b/docs/csharp/misc/cs0578.md deleted file mode 100644 index 58ef92adcdf2e..0000000000000 --- a/docs/csharp/misc/cs0578.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS0578" -title: "Compiler Error CS0578" -ms.date: 07/20/2015 -f1_keywords: - - "CS0578" -helpviewer_keywords: - - "CS0578" -ms.assetid: fbc5c884-e358-48f1-aeac-5ec8fd9f0c40 ---- -# Compiler Error CS0578 - -The Conditional attribute is not valid on 'function' because its return type is not void - - cannot be applied to a method that has a return type other than `void`. The reason for this is that any other return type for a method may be needed by another part of your program. - -## Example - -The following sample generates CS0578. To resolve this error, you must either delete , or you must change the return value of the method to `void`. - -```csharp -// CS0578.cs -// compile with: /target:library -public class MyClass -{ - [System.Diagnostics.ConditionalAttribute("a")] // CS0578 - public int TestMethod() - { - return 0; - } -} -``` diff --git a/docs/csharp/misc/cs0582.md b/docs/csharp/misc/cs0582.md deleted file mode 100644 index 9d3ad889e36a5..0000000000000 --- a/docs/csharp/misc/cs0582.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -description: "Compiler Error CS0582" -title: "Compiler Error CS0582" -ms.date: 07/20/2015 -f1_keywords: - - "CS0582" -helpviewer_keywords: - - "CS0582" -ms.assetid: cc0f4c75-c41d-423e-a4dc-e55a124f5cae ---- -# Compiler Error CS0582 - -The Conditional not valid on interface members - - **ConditionalAttribute** is not valid on an interface member. - - The following sample generates CS0582: - -```csharp -// CS0582.cs -// compile with: /target:library -using System.Diagnostics; -interface MyIFace -{ - [ConditionalAttribute("DEBUG")] // CS0582 - void zz(); -} -``` diff --git a/docs/csharp/misc/cs0609.md b/docs/csharp/misc/cs0609.md deleted file mode 100644 index 7d90c4c7a71ad..0000000000000 --- a/docs/csharp/misc/cs0609.md +++ /dev/null @@ -1,62 +0,0 @@ ---- -description: "Compiler Error CS0609" -title: "Compiler Error CS0609" -ms.date: 07/20/2015 -f1_keywords: - - "CS0609" -helpviewer_keywords: - - "CS0609" -ms.assetid: f3ff07a6-1190-4a1c-86a5-f607e1a32b78 ---- -# Compiler Error CS0609 - -Cannot set the IndexerName attribute on an indexer marked override - - The name attribute (**IndexerNameAttribute**) cannot be applied to an indexed property that is an override. For more information, see [Indexers](../programming-guide/indexers/index.md). - - The following sample generates CS0609: - -```csharp -// CS0609.cs -using System; -using System.Runtime.CompilerServices; - -public class idx -{ - public virtual int this[int iPropIndex] - { - get - { - return 0; - } - set - { - } - } -} - -public class MonthDays : idx -{ - [IndexerName("MonthInfoIndexer")] // CS0609, delete to resolve this CS0609 - public override int this[int iPropIndex] - { - get - { - return 0; - } - set - { - } - } -} - -public class test -{ - public static void Main(string[] args) - { - } -} -``` - -> [!NOTE] -> This compiler error is no longer used in Roslyn. The previous code should compile successfully. diff --git a/docs/csharp/misc/cs0625.md b/docs/csharp/misc/cs0625.md deleted file mode 100644 index 0ca54e2748e6c..0000000000000 --- a/docs/csharp/misc/cs0625.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS0625" -title: "Compiler Error CS0625" -ms.date: 07/20/2015 -f1_keywords: - - "CS0625" -helpviewer_keywords: - - "CS0625" -ms.assetid: 44091813-9988-436c-b35e-e24094793782 ---- -# Compiler Error CS0625 - -'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute. - -When a struct is marked with an explicit **StructLayout** attribute, all fields in the struct must have the [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute. For more information, see [StructLayoutAttribute Class](xref:System.Runtime.InteropServices.StructLayoutAttribute). - -The following sample generates CS0625: - -```csharp -// CS0625.cs -// compile with: /target:library -using System; -using System.Runtime.InteropServices; - -[StructLayout(LayoutKind.Explicit)] -struct A -{ - public int i; // CS0625 not static; an instance field -} - -// OK -[StructLayout(LayoutKind.Explicit)] -struct B -{ - [FieldOffset(5)] - public int i; -} -``` diff --git a/docs/csharp/misc/cs0629.md b/docs/csharp/misc/cs0629.md deleted file mode 100644 index 81921230ccba0..0000000000000 --- a/docs/csharp/misc/cs0629.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Error CS0629" -title: "Compiler Error CS0629" -ms.date: 07/20/2015 -f1_keywords: - - "CS0629" -helpviewer_keywords: - - "CS0629" -ms.assetid: 20f22ef0-3c6f-4108-ab09-3f0752375a10 ---- -# Compiler error CS0629 - -Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name' - -The [Conditional attribute](../language-reference/attributes/general.md#conditional-attribute) cannot be used on the implementation of an interface. - -The following sample generates CS0629: - -```csharp -// CS0629.cs -interface MyInterface -{ - void MyMethod(); -} - -public class MyClass : MyInterface -{ - [System.Diagnostics.Conditional("debug")] - public void MyMethod() // CS0629, remove the Conditional attribute - { - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0636.md b/docs/csharp/misc/cs0636.md deleted file mode 100644 index d2cd0bad44b93..0000000000000 --- a/docs/csharp/misc/cs0636.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Error CS0636" -title: "Compiler Error CS0636" -ms.date: 07/20/2015 -f1_keywords: - - "CS0636" -helpviewer_keywords: - - "CS0636" -ms.assetid: 47597f89-fbe6-4708-9493-3c86c6f0ce56 ---- -# Compiler Error CS0636 - -The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit) - - You must use the **StructLayout(LayoutKind.Explicit)** attribute on the struct declaration, if it contains any members marked with the **FieldOffset** attribute. For more information, see [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute). - - The following sample generates CS0636: - -```csharp -// CS0636.cs -using System; -using System.Runtime.InteropServices; - -// To resolve the error, uncomment the following line: -// [StructLayout(LayoutKind.Explicit)] -struct Worksheet -{ - [FieldOffset(4)]public int i; // CS0636 -} - -public class MainClass -{ - public static void Main () - { - } -} -``` diff --git a/docs/csharp/misc/cs0637.md b/docs/csharp/misc/cs0637.md deleted file mode 100644 index 09981850c9af7..0000000000000 --- a/docs/csharp/misc/cs0637.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS0637" -title: "Compiler Error CS0637" -ms.date: 07/20/2015 -f1_keywords: - - "CS0637" -helpviewer_keywords: - - "CS0637" -ms.assetid: 02f6964c-0fcc-4f81-8ebb-0330aecdde19 ---- -# Compiler Error CS0637 - -The FieldOffset attribute is not allowed on static or const fields. - - The [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute cannot be used on fields marked with [static](../language-reference/keywords/static.md) or [const](../language-reference/keywords/const.md). - - The following sample generates CS0637: - -```csharp -// CS0637.cs -using System; -using System.Runtime.InteropServices; - -[StructLayout(LayoutKind.Explicit)] -public class MainClass -{ - [FieldOffset(3)] // CS0637 - public static int i; - public static void Main () - { - } -} -``` diff --git a/docs/csharp/misc/cs0641.md b/docs/csharp/misc/cs0641.md deleted file mode 100644 index 0513d688a7409..0000000000000 --- a/docs/csharp/misc/cs0641.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -description: "Compiler Error CS0641" -title: "Compiler Error CS0641" -ms.date: 07/20/2015 -f1_keywords: - - "CS0641" -helpviewer_keywords: - - "CS0641" -ms.assetid: 5bcdb11a-fefd-4c71-9b31-4c4f719633f3 ---- -# Compiler Error CS0641 - -'attribute' : attribute is only valid on classes derived from System.Attribute - - An attribute was used that can only be used on a class that derives from **System.Attribute**. - - The following sample generates CS0641: - -```csharp -// CS0641.cs -using System; - -[AttributeUsage(AttributeTargets.All)] -public class NonAttrClass // CS0641 -// try the following line instead -// public class NonAttrClass : Attribute -{ -} - -class MyClass -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0646.md b/docs/csharp/misc/cs0646.md deleted file mode 100644 index e715835aa12c5..0000000000000 --- a/docs/csharp/misc/cs0646.md +++ /dev/null @@ -1,63 +0,0 @@ ---- -description: "Compiler Error CS0646" -title: "Compiler Error CS0646" -ms.date: 07/20/2015 -f1_keywords: - - "CS0646" -helpviewer_keywords: - - "CS0646" -ms.assetid: 48ea306f-b4a0-4988-8d2b-ca9d38e9bdad ---- -# Compiler Error CS0646 - -Cannot specify the DefaultMember attribute on a type containing an indexer - - If a class or other type specifies **System.Reflection.DefaultMemberAttribute**, it cannot contain an indexer. For more information, see [Properties](../programming-guide/classes-and-structs/properties.md). - - The following sample generates CS0646: - -```csharp -// CS0646.cs -// compile with: /target:library -[System.Reflection.DefaultMemberAttribute("x")] // CS0646 -class MyClass -{ - public int this[int index] // an indexer - { - get - { - return 0; - } - } - - public int x = 0; -} - -// OK -[System.Reflection.DefaultMemberAttribute("x")] -class MyClass2 -{ - public int prop - { - get - { - return 0; - } - } - - public int x = 0; -} - -class MyClass3 -{ - public int this[int index] // an indexer - { - get - { - return 0; - } - } - - public int x = 0; -} -``` diff --git a/docs/csharp/misc/cs0653.md b/docs/csharp/misc/cs0653.md deleted file mode 100644 index da78e17fff1c0..0000000000000 --- a/docs/csharp/misc/cs0653.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0653" -title: "Compiler Error CS0653" -ms.date: 07/20/2015 -f1_keywords: - - "CS0653" -helpviewer_keywords: - - "CS0653" -ms.assetid: c94043b9-b5d9-4294-921d-a4aead124d44 ---- -# Compiler Error CS0653 - -Cannot apply attribute class 'class' because it is abstract - - An [abstract](../language-reference/keywords/abstract.md) custom attribute class cannot be used as an attribute. - - The following sample generates CS0653: - -```csharp -// CS0653.cs -using System; - -public abstract class MyAttribute : Attribute -{ -} - -public class My2Attribute : MyAttribute -{ -} - -[My] // CS0653 -// try the following line instead -// [My2] -class MyClass -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0657.md b/docs/csharp/misc/cs0657.md deleted file mode 100644 index 5e8fa8db48d07..0000000000000 --- a/docs/csharp/misc/cs0657.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS0657" -title: "Compiler Warning (level 1) CS0657" -ms.date: 07/20/2015 -f1_keywords: - - "CS0657" -helpviewer_keywords: - - "CS0657" -ms.assetid: d12d2efc-f44e-40e6-b825-5a66ead0c08e ---- -# Compiler Warning (level 1) CS0657 - -'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored. - - The compiler found an attribute modifier in an invalid location. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. - - The following sample generates CS0657: - -```csharp -// CS0657.cs -// compile with: /target:library -public class TestAttribute : System.Attribute {} -[return: Test] // CS0657 return not valid on a class -class Class1 {} -``` diff --git a/docs/csharp/misc/cs0658.md b/docs/csharp/misc/cs0658.md deleted file mode 100644 index ef34d72c2451c..0000000000000 --- a/docs/csharp/misc/cs0658.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Warning (level 1) CS0658" -title: "Compiler Warning (level 1) CS0658" -ms.date: 07/20/2015 -f1_keywords: - - "CS0658" -helpviewer_keywords: - - "CS0658" -ms.assetid: 0309074c-741a-492c-9370-73b4bbfd3c1a ---- -# Compiler Warning (level 1) CS0658 - -'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored. - - An invalid attribute modifier was specified. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. - - The following sample generates CS0658: - -```csharp -// CS0658.cs -using System; -public class TestAttribute : Attribute {} -[badAttributeLocation: Test] // CS0658, badAttributeLocation is invalid -class ClassTest -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0685.md b/docs/csharp/misc/cs0685.md deleted file mode 100644 index 655356e48c18e..0000000000000 --- a/docs/csharp/misc/cs0685.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS0685" -title: "Compiler Error CS0685" -ms.date: 07/20/2015 -f1_keywords: - - "CS0685" -helpviewer_keywords: - - "CS0685" -ms.assetid: 20d7c6cf-a388-430f-b22b-232536912491 ---- -# Compiler Error CS0685 - -Conditional member 'member' cannot have an out parameter - - When using the attribute on a method, that method may not have an out parameter. This is because the value of the variable used for the out parameter would not be defined in the case that the method call is compiled to nothing. To avoid this error, remove the out parameter from the conditional method declaration, or don't use the Conditional Attribute. - -## Example - - The following sample generates CS0685: - -```csharp -// CS0685.cs -using System.Diagnostics; - -class C -{ - [Conditional("DEBUG")] - void trace(out int i) // CS0685 - { - i = 1; - } -} -``` From 7aeb8c0437555f5fff09cab4e83c915a74c81d0a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 12:58:54 -0500 Subject: [PATCH 03/10] Add additional undocumented errors --- .../attribute-usage-errors.md | 36 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 3 +- ...n-t-have-specifics-on-this-csharp-error.md | 4 --- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index a67ea306d5502..1e4b81db90631 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -21,6 +21,10 @@ f1_keywords: - "CS0657" - "CS0658" - "CS0685" + - "CS7014" + - "CS7046" + - "CS7047" + - "CS7067" - "CS9331" helpviewer_keywords: - "CS0243" @@ -42,6 +46,10 @@ helpviewer_keywords: - "CS0657" - "CS0658" - "CS0685" + - "CS7014" + - "CS7046" + - "CS7047" + - "CS7067" - "CS9331" ms.date: 11/11/2025 ai-usage: ai-assisted @@ -72,6 +80,10 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0657**](#invalid-attribute-location): *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* - [**CS0658**](#unrecognized-attribute-location): *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* - [**CS0685**](#conditional-with-out-parameter): *Conditional member 'member' cannot have an out parameter* +- [**CS7014**](#attributes-not-allowed): *Attributes are not valid in this context.* +- [**CS7046**](#attribute-parameter-required): *Attribute parameter '{0}' must be specified.* +- [**CS7047**](#attribute-parameters-required): *Attribute parameter '{0}' or '{1}' must be specified.* +- [**CS7067**](#attribute-parameter-default-argument): *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* - [**CS9331**](#reserved-attributes): *Attribute cannot be applied manually.* ## Conditional attribute on override @@ -624,6 +636,30 @@ class C } ``` +## Attributes not allowed + +- **CS7014**: *Attributes are not valid in this context.* + +Attributes can only be applied to specific declaration contexts. This error occurs when you attempt to apply an attribute in a location where attributes are not syntactically valid. + +## Attribute parameter required + +- **CS7046**: *Attribute parameter '{0}' must be specified.* + +Some attributes require specific parameters to be provided. This error occurs when you apply an attribute but fail to specify a required parameter value. + +## Attribute parameters required + +- **CS7047**: *Attribute parameter '{0}' or '{1}' must be specified.* + +Some attributes require that at least one of multiple possible parameters must be provided. This error occurs when you apply an attribute but fail to specify any of the required parameter values. + +## Attribute parameter default argument + +- **CS7067**: *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* + +When an attribute constructor parameter is marked as optional, it must have a default value specified. This error occurs when you declare an optional parameter in an attribute constructor without providing a default value. + ## Reserved attribute - **CS9331**: *Attribute cannot be applied manually.* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 687326b39dd2d..ac5e564fed106 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -470,7 +470,8 @@ items: href: ./compiler-messages/attribute-usage-errors.md displayName: > CS0243, CS0404, CS0415, CS0416, CS0447, CS0577, CS0578, CS0582, CS0609, CS0625, - CS0629, CS0636, CS0637, CS0641, CS0646, CS0653, CS0657, CS0658, CS0685, CS9331 + CS0629, CS0636, CS0637, CS0641, CS0646, CS0653, CS0657, CS0658, CS0685, CS7014, + CS7046, CS7047, CS7067, CS9331 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 6f8b8c95ce343..7a25597850dd4 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -82,7 +82,6 @@ f1_keywords: - "CS7006" - "CS7012" - "CS7013" - - "CS7014" - "CS7015" - "CS7016" - "CS7017" @@ -107,8 +106,6 @@ f1_keywords: - "CS7042" - "CS7043" - "CS7045" - - "CS7046" - - "CS7047" - "CS7048" - "CS7049" - "CS7050" @@ -124,7 +121,6 @@ f1_keywords: - "CS7064" - "CS7065" - "CS7066" - - "CS7067" - "CS7070" - "CS7080" - "CS7081" From 288d552cb20394eb3b8eafe93e5986e88e4dc239 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 13:26:55 -0500 Subject: [PATCH 04/10] First edit pass. --- .github/prompts/error-consolidation.md | 5 + .../attribute-usage-errors.md | 652 +++--------------- 2 files changed, 89 insertions(+), 568 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index e3dc05181a9c0..041044b0f457a 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -61,3 +61,8 @@ To do that, make a new H2 section for the theme. Remove all the H2s for the indi The list of errors at the top of the file should remain in numerical order, so it's easy for readers to scan. Each impacted error code should now have a link to the anchor tag for the new section. Repeat the list in the new section, but without the anchors, as shown in the highlighted text. Understand these instructions, then suggest a list of themes and the included error codes. I'll approve each theme before you begin editing. + +## Move from description to resolution + +Rework the highlighted section so the focus is on how to correct each error. This article doesn't need to explain the the associated language feature. Instead, in each section, provide links to language reference or language specification material that explains the rules violated when these diagnostics appear. Add explanatory context after each correction (in parentheses with the error code). Provided brief reasons why each correction is needed. Use detailed, sentence-style explanations rather than brief imperative statements. For each recommendation put the affectived error codes in parentheses, and in **bold** style. Remove extensive examples. + diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 1e4b81db90631..7a5c5b45250e1 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -61,607 +61,123 @@ This article covers the following compiler errors: -- [**CS0243**](#conditional-attribute-on-override): *The Conditional attribute is not valid on 'method' because it is an override method* -- [**CS0404**](#generic-attributes): *'<' unexpected : attributes cannot be generic* -- [**CS0415**](#indexername-attribute): *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* -- [**CS0416**](#attribute-argument-type-parameters): *'type parameter': an attribute argument cannot use type parameters* -- [**CS0447**](#attributes-on-type-arguments): *Attributes cannot be used on type arguments, only on type parameters* -- [**CS0577**](#conditional-attribute-restrictions): *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* -- [**CS0578**](#conditional-attribute-return-type): *The Conditional attribute is not valid on 'function' because its return type is not void* -- [**CS0582**](#conditional-on-interface-members): *The Conditional not valid on interface members* -- [**CS0609**](#indexername-on-override): *Cannot set the IndexerName attribute on an indexer marked override* -- [**CS0625**](#fieldoffset-attribute-required): *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* -- [**CS0629**](#conditional-on-interface-implementation): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* -- [**CS0636**](#structlayout-required): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* -- [**CS0637**](#fieldoffset-on-static-const): *The FieldOffset attribute is not allowed on static or const fields.* -- [**CS0641**](#attributeusage-attribute): *'attribute' : attribute is only valid on classes derived from System.Attribute* -- [**CS0646**](#defaultmember-attribute): *Cannot specify the DefaultMember attribute on a type containing an indexer* -- [**CS0653**](#abstract-attribute-class): *Cannot apply attribute class 'class' because it is abstract* -- [**CS0657**](#invalid-attribute-location): *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* -- [**CS0658**](#unrecognized-attribute-location): *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* -- [**CS0685**](#conditional-with-out-parameter): *Conditional member 'member' cannot have an out parameter* -- [**CS7014**](#attributes-not-allowed): *Attributes are not valid in this context.* -- [**CS7046**](#attribute-parameter-required): *Attribute parameter '{0}' must be specified.* -- [**CS7047**](#attribute-parameters-required): *Attribute parameter '{0}' or '{1}' must be specified.* -- [**CS7067**](#attribute-parameter-default-argument): *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* -- [**CS9331**](#reserved-attributes): *Attribute cannot be applied manually.* - -## Conditional attribute on override +- [**CS0243**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'method' because it is an override method* +- [**CS0404**](#attribute-class-requirements): *'<' unexpected : attributes cannot be generic* +- [**CS0415**](#predefined-attributes): *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* +- [**CS0416**](#attribute-arguments-parameters): *'type parameter': an attribute argument cannot use type parameters* +- [**CS0447**](#attribute-arguments-parameters): *Attributes cannot be used on type arguments, only on type parameters* +- [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* +- [**CS0578**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because its return type is not void* +- [**CS0582**](#conditional-attribute-usage): *The Conditional not valid on interface members* +- [**CS0609**](#predefined-attributes): *Cannot set the IndexerName attribute on an indexer marked override* +- [**CS0625**](#predefined-attributes): *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- [**CS0629**](#conditional-attribute-usage): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* +- [**CS0636**](#predefined-attributes): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* +- [**CS0637**](#predefined-attributes): *The FieldOffset attribute is not allowed on static or const fields.* +- [**CS0641**](#attribute-class-requirements): *'attribute' : attribute is only valid on classes derived from System.Attribute* +- [**CS0646**](#predefined-attributes): *Cannot specify the DefaultMember attribute on a type containing an indexer* +- [**CS0653**](#attribute-class-requirements): *Cannot apply attribute class 'class' because it is abstract* +- [**CS0657**](#attribute-location-context): *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* +- [**CS0658**](#attribute-location-context): *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* +- [**CS0685**](#conditional-attribute-usage): *Conditional member 'member' cannot have an out parameter* +- [**CS7014**](#attribute-location-context): *Attributes are not valid in this context.* +- [**CS7046**](#attribute-arguments-parameters): *Attribute parameter '{0}' must be specified.* +- [**CS7047**](#attribute-arguments-parameters): *Attribute parameter '{0}' or '{1}' must be specified.* +- [**CS7067**](#attribute-arguments-parameters): *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* +- [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.* + +## Conditional attribute usage + +The following errors occur when you apply the in ways that violate its usage restrictions: - **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method* - -The attribute is not allowed on a method that is marked with the [override](../keywords/override.md) keyword. For more information, see [Knowing When to Use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). - -The compiler never binds to override methods. It only binds to the base method, and the common language runtime calls the override, as appropriate. - -The following code generates CS0243: - -```csharp -// CS0243.cs -// compile with: /target:library -public class MyClass -{ - public virtual void M() {} -} - -public class MyClass2 : MyClass -{ - [System.Diagnostics.ConditionalAttribute("MySymbol")] // CS0243 - // remove Conditional attribute or remove override keyword - public override void M() {} -} -``` - -## Generic attributes - -- **CS0404**: *'<' unexpected : attributes cannot be generic* - -Generic type parameters are not allowed in attributes. Remove the type parameter and angled brackets. - -The following sample generates CS0404: - -```csharp -// CS0404.cs -[MyAttrib] // CS0404 -class C -{ - public static void Main() - { - - } -} -``` - -## IndexerName attribute - -- **CS0415**: *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* - -This error occurs if you use an IndexerName attribute on an indexer that was an explicit implementation of an interface. This error may be avoided by removing the interface name from the declaration of the indexer, if possible. For more information, see the [IndexerNameAttribute Class](xref:System.Runtime.CompilerServices.IndexerNameAttribute). - -The following sample generates CS0415: - -```csharp -// CS0415.cs -using System; -using System.Runtime.CompilerServices; - -public interface IA -{ - int this[int index] - { - get; - set; - } -} - -public class A : IA -{ - [IndexerName("Item")] // CS0415 - int IA.this[int index] - // Try this line instead: - // public int this[int index] - { - get { return 0; } - set { } - } - - static void Main() - { - } -} -``` - -## Attribute argument type parameters - -- **CS0416**: *'type parameter': an attribute argument cannot use type parameters* - -A type parameter was used as an attribute argument, which is not allowed. Use a non-generic type. - -The following sample generates CS0416: - -```csharp -// CS0416.cs -public class MyAttribute : System.Attribute -{ - public MyAttribute(System.Type t) - { - } -} - -class G -{ - - [MyAttribute(typeof(G))] // CS0416 - public void F() - { - } - -} -``` - -## Attributes on type arguments - -- **CS0447**: *Attributes cannot be used on type arguments, only on type parameters* - -This error occurs when you apply an attribute to a type argument that occurs in an invocation statement. It is acceptable to apply an attribute to a type parameter in a class or method declaration statement such as the following: - -```csharp -class C<[some attribute] T> {…} -``` - -The following line of code will generate this error. It is assumed that the class `C`, defined in the previous line of code, has a static method called `MyStaticMethod`. - -```csharp -C<[some attribute] T>.MyStaticMethod(); -``` - -The following code generates error CS0447: - -```csharp -// CS0447.cs -using System; - -namespace Test41 -{ - public interface I - { - void Meth(); - } - public class B : I - { - void I<[Test] int>.Meth() { } // CS0447 - } -} -``` - -> [!NOTE] -> This compiler error is no longer used in Roslyn. - -## Conditional attribute restrictions - - **CS0577**: *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* - -> [!NOTE] -> Destructor is a deprecated term for [finalizer](../../programming-guide/classes-and-structs/finalizers.md). - -`Conditional` cannot be applied to the specified methods. - -For example, you cannot use some attributes on an explicit interface definition. The following sample generates CS0577: - -```csharp -// CS0577.cs -// compile with: /target:library -interface I -{ - void m(); -} - -public class MyClass : I -{ - [System.Diagnostics.Conditional("a")] // CS0577 - void I.m() {} -} -``` - -## Conditional attribute return type - - **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void* - - cannot be applied to a method that has a return type other than `void`. The reason for this is that any other return type for a method may be needed by another part of your program. - -The following sample generates CS0578. To resolve this error, you must either delete , or you must change the return value of the method to `void`. - -```csharp -// CS0578.cs -// compile with: /target:library -public class MyClass -{ - [System.Diagnostics.ConditionalAttribute("a")] // CS0578 - public int TestMethod() - { - return 0; - } -} -``` - -## Conditional on interface members - - **CS0582**: *The Conditional not valid on interface members* - -**ConditionalAttribute** is not valid on an interface member. - -The following sample generates CS0582: - -```csharp -// CS0582.cs -// compile with: /target:library -using System.Diagnostics; -interface MyIFace -{ - [ConditionalAttribute("DEBUG")] // CS0582 - void zz(); -} -``` - -## IndexerName on override - -- **CS0609**: *Cannot set the IndexerName attribute on an indexer marked override* - -The name attribute (**IndexerNameAttribute**) cannot be applied to an indexed property that is an override. For more information, see [Indexers](../../programming-guide/indexers/index.md). - -The following sample generates CS0609: - -```csharp -// CS0609.cs -using System; -using System.Runtime.CompilerServices; - -public class idx -{ - public virtual int this[int iPropIndex] - { - get - { - return 0; - } - set - { - } - } -} - -public class MonthDays : idx -{ - [IndexerName("MonthInfoIndexer")] // CS0609, delete to resolve this CS0609 - public override int this[int iPropIndex] - { - get - { - return 0; - } - set - { - } - } -} - -public class test -{ - public static void Main(string[] args) - { - } -} -``` - -> [!NOTE] -> This compiler error is no longer used in Roslyn. The previous code should compile successfully. - -## FieldOffset attribute required - -- **CS0625**: *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* - -When a struct is marked with an explicit **StructLayout** attribute, all fields in the struct must have the [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute. For more information, see [StructLayoutAttribute Class](xref:System.Runtime.InteropServices.StructLayoutAttribute). - -The following sample generates CS0625: - -```csharp -// CS0625.cs -// compile with: /target:library -using System; -using System.Runtime.InteropServices; - -[StructLayout(LayoutKind.Explicit)] -struct A -{ - public int i; // CS0625 not static; an instance field -} - -// OK -[StructLayout(LayoutKind.Explicit)] -struct B -{ - [FieldOffset(5)] - public int i; -} -``` - -## Conditional on interface implementation - - **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* +- **CS0685**: *Conditional member 'member' cannot have an out parameter* -The attribute cannot be used on the implementation of an interface. - -The following sample generates CS0629: - -```csharp -// CS0629.cs -interface MyInterface -{ - void MyMethod(); -} - -public class MyClass : MyInterface -{ - [System.Diagnostics.Conditional("debug")] - public void MyMethod() // CS0629, remove the Conditional attribute - { - } - - public static void Main() - { - } -} -``` - -## StructLayout required - -- **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* - -You must use the **StructLayout(LayoutKind.Explicit)** attribute on the struct declaration, if it contains any members marked with the **FieldOffset** attribute. For more information, see [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute). +To correct these errors, follow these rules. For more information, see and [Attributes](../../advanced-topics/reflection-and-attributes/index.md). -The following sample generates CS0636: +- Remove the `Conditional` attribute from [override](../keywords/override.md) methods, or remove the `override` keyword (**CS0243**). The compiler binds to the base method, not the override. +- Don't apply `Conditional` to constructors, [finalizers](../../programming-guide/classes-and-structs/finalizers.md), operators, or explicit interface implementations (**CS0577**). These member types don't support conditional compilation. +- Change the method's return type to `void`, or remove the `Conditional` attribute (**CS0578**). Conditional methods must return `void` because calls may be compiled out. +- Remove the `Conditional` attribute from interface member declarations (**CS0582**). Interface members can't be conditional. +- Remove the `Conditional` attribute from interface member implementations (**CS0629**). Methods implementing interface members can't be conditional. +- Remove `out` parameters from conditional methods, or remove the `Conditional` attribute (**CS0685**). The `out` variable value would be undefined when the method call is compiled out. -```csharp -// CS0636.cs -using System; -using System.Runtime.InteropServices; +## Attribute arguments parameters -// To resolve the error, uncomment the following line: -// [StructLayout(LayoutKind.Explicit)] -struct Worksheet -{ - [FieldOffset(4)]public int i; // CS0636 -} +The following errors occur when you use attribute arguments or parameters incorrectly: -public class MainClass -{ - public static void Main () - { - } -} -``` +- **CS0416**: *'type parameter': an attribute argument cannot use type parameters* +- **CS0447**: *Attributes cannot be used on type arguments, only on type parameters* +- **CS7046**: *Attribute parameter '{0}' must be specified.* +- **CS7047**: *Attribute parameter '{0}' or '{1}' must be specified.* +- **CS7067**: *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* -## FieldOffset on static const +To correct these errors, follow these rules. -- **CS0637**: *The FieldOffset attribute is not allowed on static or const fields.* +- Use compile-time constant values for attribute arguments instead of type parameters (**CS0416**). Attribute arguments must be evaluated at compile time. +- Apply attributes to type parameter declarations, not to type arguments in generic type uses (**CS0447**). For example, apply attributes to `` in a class declaration, not to `` when using the class. +- Provide all required attribute parameters when applying the attribute (**CS7046**, **CS7047**). Check the attribute's constructor to see which parameters are mandatory. +- Specify default values for optional constructor parameters when defining custom attributes (**CS7067**). Use the syntax `parameterType parameterName = defaultValue` in the attribute constructor. -The [FieldOffset](xref:System.Runtime.InteropServices.FieldOffsetAttribute) attribute cannot be used on fields marked with [static](../keywords/static.md) or [const](../keywords/const.md). +For more information, see [Attributes](../../advanced-topics/reflection-and-attributes/index.md) and [Generics](../../fundamentals/types/generics.md). -The following sample generates CS0637: - -```csharp -// CS0637.cs -using System; -using System.Runtime.InteropServices; +> [!NOTE] +> **CS0447** is no longer used in Roslyn. -[StructLayout(LayoutKind.Explicit)] -public class MainClass -{ - [FieldOffset(3)] // CS0637 - public static int i; - public static void Main () - { - } -} -``` +## Attribute class requirements -## AttributeUsage attribute +The following errors occur when you define attribute classes that don't meet the required constraints: +- **CS0404**: *'<' unexpected : attributes cannot be generic* - **CS0641**: *'attribute' : attribute is only valid on classes derived from System.Attribute* - -An attribute was used that can only be used on a class that derives from **System.Attribute**. - -The following sample generates CS0641: - -```csharp -// CS0641.cs -using System; - -[AttributeUsage(AttributeTargets.All)] -public class NonAttrClass // CS0641 -// try the following line instead -// public class NonAttrClass : Attribute -{ -} - -class MyClass -{ - public static void Main() - { - } -} -``` - -## DefaultMember attribute - -- **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* - -If a class or other type specifies **System.Reflection.DefaultMemberAttribute**, it cannot contain an indexer. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md). - -The following sample generates CS0646: - -```csharp -// CS0646.cs -// compile with: /target:library -[System.Reflection.DefaultMemberAttribute("x")] // CS0646 -class MyClass -{ - public int this[int index] // an indexer - { - get - { - return 0; - } - } - - public int x = 0; -} - -// OK -[System.Reflection.DefaultMemberAttribute("x")] -class MyClass2 -{ - public int prop - { - get - { - return 0; - } - } - - public int x = 0; -} - -class MyClass3 -{ - public int this[int index] // an indexer - { - get - { - return 0; - } - } - - public int x = 0; -} -``` - -## Abstract attribute class - - **CS0653**: *Cannot apply attribute class 'class' because it is abstract* -An [abstract](../keywords/abstract.md) custom attribute class cannot be used as an attribute. - -The following sample generates CS0653: +To correct these errors, follow these rules. -```csharp -// CS0653.cs -using System; +- Make the attribute class derive from (**CS0641**). All custom attributes must inherit from the `Attribute` base class. +- Remove generic type parameters from attribute class declarations (**CS0404**). Attributes can't be generic types. +- Remove the `abstract` modifier from attribute classes, or derive from a non-abstract attribute class (**CS0653**). Attributes must be instantiable. +- Apply only to classes that derive from `Attribute` (**CS0641**). This attribute controls how other attributes are used. -public abstract class MyAttribute : Attribute -{ -} + For more information, see [Create custom attributes](../../advanced-topics/reflection-and-attributes/creating-custom-attributes.md). -public class My2Attribute : MyAttribute -{ -} +## Attribute location context -[My] // CS0653 -// try the following line instead -// [My2] -class MyClass -{ - public static void Main() - { - } -} -``` - -## Invalid attribute location +The following errors occur when you apply attributes in invalid locations or with incorrect target specifiers: - **CS0657**: *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* - -The compiler found an attribute modifier in an invalid location. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. - -The following sample generates CS0657: - -```csharp -// CS0657.cs -// compile with: /target:library -public class TestAttribute : System.Attribute {} -[return: Test] // CS0657 return not valid on a class -class Class1 {} -``` - -## Unrecognized attribute location - - **CS0658**: *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* - -An invalid attribute modifier was specified. See [Attribute Targets](/dotnet/csharp/advanced-topics/reflection-and-attributes#attribute-targets) for more information. - -The following sample generates CS0658: - -```csharp -// CS0658.cs -using System; -public class TestAttribute : Attribute {} -[badAttributeLocation: Test] // CS0658, badAttributeLocation is invalid -class ClassTest -{ - public static void Main() - { - } -} -``` - -## Conditional with out parameter - -- **CS0685**: *Conditional member 'member' cannot have an out parameter* - -When using the attribute on a method, that method may not have an out parameter. This is because the value of the variable used for the out parameter would not be defined in the case that the method call is compiled to nothing. To avoid this error, remove the out parameter from the conditional method declaration, or don't use the Conditional Attribute. - -The following sample generates CS0685: - -```csharp -// CS0685.cs -using System.Diagnostics; - -class C -{ - [Conditional("DEBUG")] - void trace(out int i) // CS0685 - { - i = 1; - } -} -``` - -## Attributes not allowed - - **CS7014**: *Attributes are not valid in this context.* -Attributes can only be applied to specific declaration contexts. This error occurs when you attempt to apply an attribute in a location where attributes are not syntactically valid. - -## Attribute parameter required - -- **CS7046**: *Attribute parameter '{0}' must be specified.* - -Some attributes require specific parameters to be provided. This error occurs when you apply an attribute but fail to specify a required parameter value. - -## Attribute parameters required +To correct these errors, follow these rules. For more information, see [Attribute Targets](../../advanced-topics/reflection-and-attributes/index.md#attribute-targets). -- **CS7047**: *Attribute parameter '{0}' or '{1}' must be specified.* +- Use valid attribute target specifiers for the declaration (**CS0657**). Check the error message to see which targets are valid for the specific declaration. +- Remove invalid attribute target specifiers (**CS0658**). Valid specifiers include `assembly:`, `module:`, `type:`, `method:`, `property:`, `field:`, `event:`, `param:`, and `return:`. +- Move attributes to valid contexts (**CS7014**). Attributes can only be applied to program elements that support them, as defined by . +- Apply attributes at the appropriate scope level. Use `assembly:` or `module:` prefixes for assembly-level or module-level attributes. -Some attributes require that at least one of multiple possible parameters must be provided. This error occurs when you apply an attribute but fail to specify any of the required parameter values. +## Predefined attributes -## Attribute parameter default argument - -- **CS7067**: *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* - -When an attribute constructor parameter is marked as optional, it must have a default value specified. This error occurs when you declare an optional parameter in an attribute constructor without providing a default value. - -## Reserved attribute +The following errors occur when you use specific predefined .NET attributes incorrectly: +- **CS0415**: *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* +- **CS0609**: *Cannot set the IndexerName attribute on an indexer marked override* +- **CS0625**: *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* +- **CS0637**: *The FieldOffset attribute is not allowed on static or const fields.* +- **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* - **CS9331**: *Attribute cannot be applied manually.* -- Replace reserved attributes with the equivalent C# syntax. The compiler generates the attribute in the generated code (**CS9331**). +To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md) and [Platform Invoke (P/Invoke)](../../standard/native-interop/pinvoke.md). + +- Remove from explicit interface implementations (**CS0415**). Apply it only to public indexers. +- Remove `IndexerName` from indexers marked with `override` (**CS0609**). Override indexers inherit the name from the base class. +- Add to all instance fields in types with set to `LayoutKind.Explicit` (**CS0625**). Explicit layout requires explicit field positions. +- Apply `FieldOffset` only to types with `StructLayout(LayoutKind.Explicit)` (**CS0636**). Add the `StructLayout` attribute to the type declaration. +- Remove `FieldOffset` from `static` or `const` fields (**CS0637**). Explicit layout applies only to instance fields. +- Remove from types that contain indexers (**CS0646**). Indexers automatically define the default member. +- Replace compiler-generated attributes with the equivalent C# syntax (**CS9331**). Use language keywords instead of manually applying reserved attributes. From 4bcc2c5c2d40c575da7618fb9abb25045cb25c4c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 13:57:46 -0500 Subject: [PATCH 05/10] Final edit pass --- .../attribute-usage-errors.md | 94 +++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 7a5c5b45250e1..a1300e4a7fe67 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -62,64 +62,44 @@ This article covers the following compiler errors: That's by design. The text closely matches the text of the compiler error / warning for SEO purposes. --> - [**CS0243**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'method' because it is an override method* -- [**CS0404**](#attribute-class-requirements): *'<' unexpected : attributes cannot be generic* -- [**CS0415**](#predefined-attributes): *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* +- [**CS0404**](#attribute-class-requirements): *Attribute is not valid on this declaration type.* +- [**CS0415**](#predefined-attributes): *This attribute is valid only on an indexer that is not an explicit interface member declaration* - [**CS0416**](#attribute-arguments-parameters): *'type parameter': an attribute argument cannot use type parameters* -- [**CS0447**](#attribute-arguments-parameters): *Attributes cannot be used on type arguments, only on type parameters* -- [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* +- [**CS0447**](#attribute-arguments-parameters): *Attribute cannot be used with type arguments* +- [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation* - [**CS0578**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because its return type is not void* -- [**CS0582**](#conditional-attribute-usage): *The Conditional not valid on interface members* -- [**CS0609**](#predefined-attributes): *Cannot set the IndexerName attribute on an indexer marked override* -- [**CS0625**](#predefined-attributes): *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- [**CS0582**](#conditional-attribute-usage): *The Conditional attribute is not valid on interface members* +- [**CS0609**](#predefined-attributes): *Cannot set the attribute on an indexer marked override* +- [**CS0625**](#predefined-attributes): *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute* - [**CS0629**](#conditional-attribute-usage): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* - [**CS0636**](#predefined-attributes): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* - [**CS0637**](#predefined-attributes): *The FieldOffset attribute is not allowed on static or const fields.* -- [**CS0641**](#attribute-class-requirements): *'attribute' : attribute is only valid on classes derived from System.Attribute* +- [**CS0641**](#attribute-class-requirements): *This attribute is only valid on classes derived from System.Attribute`* - [**CS0646**](#predefined-attributes): *Cannot specify the DefaultMember attribute on a type containing an indexer* - [**CS0653**](#attribute-class-requirements): *Cannot apply attribute class 'class' because it is abstract* -- [**CS0657**](#attribute-location-context): *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* -- [**CS0658**](#attribute-location-context): *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* +- [**CS0657**](#attribute-location-context): *Location is not a valid attribute location for this declaration. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* +- [**CS0658**](#attribute-location-context): *Location is not a recognized attribute location. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* - [**CS0685**](#conditional-attribute-usage): *Conditional member 'member' cannot have an out parameter* - [**CS7014**](#attribute-location-context): *Attributes are not valid in this context.* -- [**CS7046**](#attribute-arguments-parameters): *Attribute parameter '{0}' must be specified.* -- [**CS7047**](#attribute-arguments-parameters): *Attribute parameter '{0}' or '{1}' must be specified.* -- [**CS7067**](#attribute-arguments-parameters): *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* +- [**CS7046**](#attribute-arguments-parameters): *Attribute parameter must be specified.* +- [**CS7047**](#attribute-arguments-parameters): *Attribute parameter 'parameter1' or 'parameter2' must be specified.* +- [**CS7067**](#attribute-arguments-parameters): *Attribute constructor parameter is optional, but no default parameter value was specified.* - [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.* -## Conditional attribute usage - -The following errors occur when you apply the in ways that violate its usage restrictions: - -- **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method* -- **CS0577**: *The Conditional attribute is not valid on 'function' because it is a constructor, destructor, operator, or explicit interface implementation* -- **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void* -- **CS0582**: *The Conditional not valid on interface members* -- **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* -- **CS0685**: *Conditional member 'member' cannot have an out parameter* - -To correct these errors, follow these rules. For more information, see and [Attributes](../../advanced-topics/reflection-and-attributes/index.md). - -- Remove the `Conditional` attribute from [override](../keywords/override.md) methods, or remove the `override` keyword (**CS0243**). The compiler binds to the base method, not the override. -- Don't apply `Conditional` to constructors, [finalizers](../../programming-guide/classes-and-structs/finalizers.md), operators, or explicit interface implementations (**CS0577**). These member types don't support conditional compilation. -- Change the method's return type to `void`, or remove the `Conditional` attribute (**CS0578**). Conditional methods must return `void` because calls may be compiled out. -- Remove the `Conditional` attribute from interface member declarations (**CS0582**). Interface members can't be conditional. -- Remove the `Conditional` attribute from interface member implementations (**CS0629**). Methods implementing interface members can't be conditional. -- Remove `out` parameters from conditional methods, or remove the `Conditional` attribute (**CS0685**). The `out` variable value would be undefined when the method call is compiled out. - ## Attribute arguments parameters The following errors occur when you use attribute arguments or parameters incorrectly: - **CS0416**: *'type parameter': an attribute argument cannot use type parameters* -- **CS0447**: *Attributes cannot be used on type arguments, only on type parameters* -- **CS7046**: *Attribute parameter '{0}' must be specified.* -- **CS7047**: *Attribute parameter '{0}' or '{1}' must be specified.* -- **CS7067**: *Attribute constructor parameter '{0}' is optional, but no default parameter value was specified.* +- **CS0447**: *Attribute cannot be used with type arguments* +- **CS7046**: *Attribute parameter must be specified.* +- **CS7047**: *Attribute parameter 'parameter1' or 'parameter2' must be specified.* +- **CS7067**: *Attribute constructor parameter is optional, but no default parameter value was specified.* To correct these errors, follow these rules. - Use compile-time constant values for attribute arguments instead of type parameters (**CS0416**). Attribute arguments must be evaluated at compile time. -- Apply attributes to type parameter declarations, not to type arguments in generic type uses (**CS0447**). For example, apply attributes to `` in a class declaration, not to `` when using the class. +- Don't use type arguments with attributes (**CS0447**). Type arguments aren't allowed in attribute usage. - Provide all required attribute parameters when applying the attribute (**CS7046**, **CS7047**). Check the attribute's constructor to see which parameters are mandatory. - Specify default values for optional constructor parameters when defining custom attributes (**CS7067**). Use the syntax `parameterType parameterName = defaultValue` in the attribute constructor. @@ -132,14 +112,14 @@ For more information, see [Attributes](../../advanced-topics/reflection-and-attr The following errors occur when you define attribute classes that don't meet the required constraints: -- **CS0404**: *'<' unexpected : attributes cannot be generic* -- **CS0641**: *'attribute' : attribute is only valid on classes derived from System.Attribute* +- **CS0404**: *Attribute is not valid on this declaration type.* +- **CS0641**: *This attribute is only valid on classes derived from System.Attribute`* - **CS0653**: *Cannot apply attribute class 'class' because it is abstract* To correct these errors, follow these rules. -- Make the attribute class derive from (**CS0641**). All custom attributes must inherit from the `Attribute` base class. -- Remove generic type parameters from attribute class declarations (**CS0404**). Attributes can't be generic types. +- Apply attributes only to valid declaration types (**CS0404**). Check the attribute's `AttributeUsage` to see which targets are allowed. +- Don't apply attributes to property or event accessors unless they're valid on accessors (**CS0641**). Apply them to the property or event declaration instead. - Remove the `abstract` modifier from attribute classes, or derive from a non-abstract attribute class (**CS0653**). Attributes must be instantiable. - Apply only to classes that derive from `Attribute` (**CS0641**). This attribute controls how other attributes are used. @@ -149,8 +129,8 @@ To correct these errors, follow these rules. The following errors occur when you apply attributes in invalid locations or with incorrect target specifiers: -- **CS0657**: *'attribute modifier' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'locations'. All attributes in this block will be ignored.* -- **CS0658**: *'attribute modifier' is not a recognized attribute location. All attributes in this block will be ignored.* +- **CS0657**: *Location is not a valid attribute location for this declaration. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* +- **CS0658**: *Location is not a recognized attribute location. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* - **CS7014**: *Attributes are not valid in this context.* To correct these errors, follow these rules. For more information, see [Attribute Targets](../../advanced-topics/reflection-and-attributes/index.md#attribute-targets). @@ -164,9 +144,9 @@ To correct these errors, follow these rules. For more information, see [Attribut The following errors occur when you use specific predefined .NET attributes incorrectly: -- **CS0415**: *The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration* -- **CS0609**: *Cannot set the IndexerName attribute on an indexer marked override* -- **CS0625**: *'field': instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- **CS0415**: *This attribute is valid only on an indexer that is not an explicit interface member declaration* +- **CS0609**: *Cannot set the attribute on an indexer marked override* +- **CS0625**: *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute* - **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* - **CS0637**: *The FieldOffset attribute is not allowed on static or const fields.* - **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* @@ -181,3 +161,23 @@ To correct these errors, follow these rules. For more information, see [Indexers - Remove `FieldOffset` from `static` or `const` fields (**CS0637**). Explicit layout applies only to instance fields. - Remove from types that contain indexers (**CS0646**). Indexers automatically define the default member. - Replace compiler-generated attributes with the equivalent C# syntax (**CS9331**). Use language keywords instead of manually applying reserved attributes. + +## Conditional attribute usage + +The following errors occur when you apply the in ways that violate its usage restrictions: + +- **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method* +- **CS0577**: *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation* +- **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void* +- **CS0582**: *The Conditional attribute is not valid on interface members* +- **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* +- **CS0685**: *Conditional member 'member' cannot have an out parameter* + +To correct these errors, follow these rules. For more information, see and [Attributes](../../advanced-topics/reflection-and-attributes/index.md). + +- Remove the `Conditional` attribute from [override](../keywords/override.md) methods, or remove the `override` keyword (**CS0243**). The compiler binds to the base method, not the override. +- Don't apply `Conditional` to constructors, [finalizers](../../programming-guide/classes-and-structs/finalizers.md), operators, lambda expressions, or explicit interface implementations (**CS0577**). These member types don't support conditional compilation. +- Change the method's return type to `void`, or remove the `Conditional` attribute (**CS0578**). Conditional methods must return `void` because calls may be compiled out. +- Remove the `Conditional` attribute from interface member declarations (**CS0582**). Interface members can't be conditional. +- Remove the `Conditional` attribute from interface member implementations (**CS0629**). Methods implementing interface members can't be conditional. +- Remove `out` parameters from conditional methods, or remove the `Conditional` attribute (**CS0685**). The `out` variable value would be undefined when the method call is compiled out. From 82b8f873a24da1b2f31dd9d0a120414387236ac5 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 14:06:57 -0500 Subject: [PATCH 06/10] build warning --- .../compiler-messages/attribute-usage-errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index a1300e4a7fe67..2c2c581d9941c 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -152,7 +152,7 @@ The following errors occur when you use specific predefined .NET attributes inco - **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* - **CS9331**: *Attribute cannot be applied manually.* -To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md) and [Platform Invoke (P/Invoke)](../../standard/native-interop/pinvoke.md). +To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md) and [Platform Invoke (P/Invoke)](../../../standard/native-interop/pinvoke.md). - Remove from explicit interface implementations (**CS0415**). Apply it only to public indexers. - Remove `IndexerName` from indexers marked with `override` (**CS0609**). Override indexers inherit the name from the base class. From a1f93ec601a1bbcdd146cd225f89f4694754d570 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 14:18:38 -0500 Subject: [PATCH 07/10] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .github/prompts/error-consolidation.md | 4 ++-- .../compiler-messages/attribute-usage-errors.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 041044b0f457a..c96c2f381d53d 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -31,7 +31,7 @@ For each source file I specify in this chat, you'll do the following tasks: ## Search for other related articles that may be missed. -First, let's find candidate source filees. Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve the target theme. Give me a list to review for possible additional consolidation. Don't make any edits until the originating user approves. +First, let's find candidate source files. Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve the target theme. Give me a list to review for possible additional consolidation. Don't make any edits until the originating user approves. ## Final search in roslyn source @@ -64,5 +64,5 @@ Understand these instructions, then suggest a list of themes and the included er ## Move from description to resolution -Rework the highlighted section so the focus is on how to correct each error. This article doesn't need to explain the the associated language feature. Instead, in each section, provide links to language reference or language specification material that explains the rules violated when these diagnostics appear. Add explanatory context after each correction (in parentheses with the error code). Provided brief reasons why each correction is needed. Use detailed, sentence-style explanations rather than brief imperative statements. For each recommendation put the affectived error codes in parentheses, and in **bold** style. Remove extensive examples. +Rework the highlighted section so the focus is on how to correct each error. This article doesn't need to explain the associated language feature. Instead, in each section, provide links to language reference or language specification material that explains the rules violated when these diagnostics appear. Add explanatory context after each correction (in parentheses with the error code). Provided brief reasons why each correction is needed. Use detailed, sentence-style explanations rather than brief imperative statements. For each recommendation put the affectived error codes in parentheses, and in **bold** style. Remove extensive examples. diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 2c2c581d9941c..13a72fb27cf71 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -86,7 +86,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS7067**](#attribute-arguments-parameters): *Attribute constructor parameter is optional, but no default parameter value was specified.* - [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.* -## Attribute arguments parameters +## Attribute arguments and parameters The following errors occur when you use attribute arguments or parameters incorrectly: From 3146de3c67f09bf83284ba07559583e00aef4513 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 14:26:34 -0500 Subject: [PATCH 08/10] review comments. --- .../attribute-usage-errors.md | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 13a72fb27cf71..84da426934794 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -1,6 +1,6 @@ --- -title: Resolve errors and warnings that involve attribute declaration and applying attributes in your code. -description: These compiler errors and warnings indicate errors in attribute declarations, or applying attributes in your code. +title: Resolve errors and warnings that involve attribute declaration and attribute use in your code. +description: These compiler errors and warnings indicate errors in attribute declarations, or attribute use in your code. f1_keywords: - "CS0243" - "CS0404" @@ -54,32 +54,32 @@ helpviewer_keywords: ms.date: 11/11/2025 ai-usage: ai-assisted --- -# Resolve errors and warnings related to attribute declarations or applying attributes in your code +# Resolve errors and warnings related to attribute declarations or attribute use in your code This article covers the following compiler errors: -- [**CS0243**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'method' because it is an override method* +- [**CS0243**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'method' because it is an override method.* - [**CS0404**](#attribute-class-requirements): *Attribute is not valid on this declaration type.* -- [**CS0415**](#predefined-attributes): *This attribute is valid only on an indexer that is not an explicit interface member declaration* -- [**CS0416**](#attribute-arguments-parameters): *'type parameter': an attribute argument cannot use type parameters* -- [**CS0447**](#attribute-arguments-parameters): *Attribute cannot be used with type arguments* -- [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation* -- [**CS0578**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because its return type is not void* -- [**CS0582**](#conditional-attribute-usage): *The Conditional attribute is not valid on interface members* -- [**CS0609**](#predefined-attributes): *Cannot set the attribute on an indexer marked override* -- [**CS0625**](#predefined-attributes): *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute* -- [**CS0629**](#conditional-attribute-usage): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* -- [**CS0636**](#predefined-attributes): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* +- [**CS0415**](#predefined-attributes): *This attribute is valid only on an indexer that is not an explicit interface member declaration.* +- [**CS0416**](#attribute-arguments-parameters): *'type parameter': an attribute argument cannot use type parameters.* +- [**CS0447**](#attribute-arguments-parameters): *Attribute cannot be used with type arguments.* +- [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation.* +- [**CS0578**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because its return type is not void.* +- [**CS0582**](#conditional-attribute-usage): *The Conditional attribute is not valid on interface members.* +- [**CS0609**](#predefined-attributes): *Cannot set the attribute on an indexer marked override.* +- [**CS0625**](#predefined-attributes): *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- [**CS0629**](#conditional-attribute-usage): *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'.* +- [**CS0636**](#predefined-attributes): *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit).* - [**CS0637**](#predefined-attributes): *The FieldOffset attribute is not allowed on static or const fields.* -- [**CS0641**](#attribute-class-requirements): *This attribute is only valid on classes derived from System.Attribute`* -- [**CS0646**](#predefined-attributes): *Cannot specify the DefaultMember attribute on a type containing an indexer* -- [**CS0653**](#attribute-class-requirements): *Cannot apply attribute class 'class' because it is abstract* +- [**CS0641**](#attribute-class-requirements): *This attribute is only valid on classes derived from System.Attribute`.* +- [**CS0646**](#predefined-attributes): *Cannot specify the DefaultMember attribute on a type containing an indexer.* +- [**CS0653**](#attribute-class-requirements): *Cannot apply attribute class 'class' because it is abstract.* - [**CS0657**](#attribute-location-context): *Location is not a valid attribute location for this declaration. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* - [**CS0658**](#attribute-location-context): *Location is not a recognized attribute location. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* -- [**CS0685**](#conditional-attribute-usage): *Conditional member 'member' cannot have an out parameter* +- [**CS0685**](#conditional-attribute-usage): *Conditional member 'member' cannot have an out parameter.* - [**CS7014**](#attribute-location-context): *Attributes are not valid in this context.* - [**CS7046**](#attribute-arguments-parameters): *Attribute parameter must be specified.* - [**CS7047**](#attribute-arguments-parameters): *Attribute parameter 'parameter1' or 'parameter2' must be specified.* @@ -90,8 +90,8 @@ That's by design. The text closely matches the text of the compiler error / warn The following errors occur when you use attribute arguments or parameters incorrectly: -- **CS0416**: *'type parameter': an attribute argument cannot use type parameters* -- **CS0447**: *Attribute cannot be used with type arguments* +- **CS0416**: *'type parameter': an attribute argument cannot use type parameters.* +- **CS0447**: *Attribute cannot be used with type arguments.* - **CS7046**: *Attribute parameter must be specified.* - **CS7047**: *Attribute parameter 'parameter1' or 'parameter2' must be specified.* - **CS7067**: *Attribute constructor parameter is optional, but no default parameter value was specified.* @@ -113,8 +113,8 @@ For more information, see [Attributes](../../advanced-topics/reflection-and-attr The following errors occur when you define attribute classes that don't meet the required constraints: - **CS0404**: *Attribute is not valid on this declaration type.* -- **CS0641**: *This attribute is only valid on classes derived from System.Attribute`* -- **CS0653**: *Cannot apply attribute class 'class' because it is abstract* +- **CS0641**: *This attribute is only valid on classes derived from System.Attribute`.* +- **CS0653**: *Cannot apply attribute class 'class' because it is abstract.* To correct these errors, follow these rules. @@ -144,12 +144,12 @@ To correct these errors, follow these rules. For more information, see [Attribut The following errors occur when you use specific predefined .NET attributes incorrectly: -- **CS0415**: *This attribute is valid only on an indexer that is not an explicit interface member declaration* -- **CS0609**: *Cannot set the attribute on an indexer marked override* -- **CS0625**: *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute* -- **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)* +- **CS0415**: *This attribute is valid only on an indexer that is not an explicit interface member declaration.* +- **CS0609**: *Cannot set the attribute on an indexer marked override.* +- **CS0625**: *Instance field in types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.* +- **CS0636**: *The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit).* - **CS0637**: *The FieldOffset attribute is not allowed on static or const fields.* -- **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer* +- **CS0646**: *Cannot specify the DefaultMember attribute on a type containing an indexer.* - **CS9331**: *Attribute cannot be applied manually.* To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md) and [Platform Invoke (P/Invoke)](../../../standard/native-interop/pinvoke.md). @@ -166,12 +166,12 @@ To correct these errors, follow these rules. For more information, see [Indexers The following errors occur when you apply the in ways that violate its usage restrictions: -- **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method* -- **CS0577**: *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation* -- **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void* -- **CS0582**: *The Conditional attribute is not valid on interface members* -- **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'* -- **CS0685**: *Conditional member 'member' cannot have an out parameter* +- **CS0243**: *The Conditional attribute is not valid on 'method' because it is an override method.* +- **CS0577**: *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation.* +- **CS0578**: *The Conditional attribute is not valid on 'function' because its return type is not void.* +- **CS0582**: *The Conditional attribute is not valid on interface members.* +- **CS0629**: *Conditional member 'member' cannot implement interface member 'base class member' in type 'Type Name'.* +- **CS0685**: *Conditional member 'member' cannot have an out parameter.* To correct these errors, follow these rules. For more information, see and [Attributes](../../advanced-topics/reflection-and-attributes/index.md). From 523f76542e7ceebed061eb89441f0f10c0424969 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 14:31:19 -0500 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../compiler-messages/attribute-usage-errors.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index 84da426934794..c5baf781507a3 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -100,8 +100,8 @@ To correct these errors, follow these rules. - Use compile-time constant values for attribute arguments instead of type parameters (**CS0416**). Attribute arguments must be evaluated at compile time. - Don't use type arguments with attributes (**CS0447**). Type arguments aren't allowed in attribute usage. -- Provide all required attribute parameters when applying the attribute (**CS7046**, **CS7047**). Check the attribute's constructor to see which parameters are mandatory. -- Specify default values for optional constructor parameters when defining custom attributes (**CS7067**). Use the syntax `parameterType parameterName = defaultValue` in the attribute constructor. +- Provide all required attribute parameters when you apply the attribute (**CS7046**, **CS7047**). Check the attribute's constructor to see which parameters are mandatory. +- Specify default values for optional constructor parameters when you define custom attributes (**CS7067**). Use the syntax `parameterType parameterName = defaultValue` in the attribute constructor. For more information, see [Attributes](../../advanced-topics/reflection-and-attributes/index.md) and [Generics](../../fundamentals/types/generics.md). @@ -119,7 +119,6 @@ The following errors occur when you define attribute classes that don't meet the To correct these errors, follow these rules. - Apply attributes only to valid declaration types (**CS0404**). Check the attribute's `AttributeUsage` to see which targets are allowed. -- Don't apply attributes to property or event accessors unless they're valid on accessors (**CS0641**). Apply them to the property or event declaration instead. - Remove the `abstract` modifier from attribute classes, or derive from a non-abstract attribute class (**CS0653**). Attributes must be instantiable. - Apply only to classes that derive from `Attribute` (**CS0641**). This attribute controls how other attributes are used. From 4f90f9c63a3814873db90379cde39224f18773a4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 Nov 2025 16:16:04 -0500 Subject: [PATCH 10/10] fix build warnings. --- .../compiler-messages/attribute-usage-errors.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md index c5baf781507a3..e2a01a4666199 100644 --- a/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md +++ b/docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md @@ -64,8 +64,8 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0243**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'method' because it is an override method.* - [**CS0404**](#attribute-class-requirements): *Attribute is not valid on this declaration type.* - [**CS0415**](#predefined-attributes): *This attribute is valid only on an indexer that is not an explicit interface member declaration.* -- [**CS0416**](#attribute-arguments-parameters): *'type parameter': an attribute argument cannot use type parameters.* -- [**CS0447**](#attribute-arguments-parameters): *Attribute cannot be used with type arguments.* +- [**CS0416**](#attribute-arguments-and-parameters): *'type parameter': an attribute argument cannot use type parameters.* +- [**CS0447**](#attribute-arguments-and-parameters): *Attribute cannot be used with type arguments.* - [**CS0577**](#conditional-attribute-usage): *The Conditional attribute is not valid because it is a constructor, destructor, operator, lambda expression, or explicit interface implementation.* - [**CS0578**](#conditional-attribute-usage): *The Conditional attribute is not valid on 'function' because its return type is not void.* - [**CS0582**](#conditional-attribute-usage): *The Conditional attribute is not valid on interface members.* @@ -81,9 +81,9 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0658**](#attribute-location-context): *Location is not a recognized attribute location. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.* - [**CS0685**](#conditional-attribute-usage): *Conditional member 'member' cannot have an out parameter.* - [**CS7014**](#attribute-location-context): *Attributes are not valid in this context.* -- [**CS7046**](#attribute-arguments-parameters): *Attribute parameter must be specified.* -- [**CS7047**](#attribute-arguments-parameters): *Attribute parameter 'parameter1' or 'parameter2' must be specified.* -- [**CS7067**](#attribute-arguments-parameters): *Attribute constructor parameter is optional, but no default parameter value was specified.* +- [**CS7046**](#attribute-arguments-and-parameters): *Attribute parameter must be specified.* +- [**CS7047**](#attribute-arguments-and-parameters): *Attribute parameter 'parameter1' or 'parameter2' must be specified.* +- [**CS7067**](#attribute-arguments-and-parameters): *Attribute constructor parameter is optional, but no default parameter value was specified.* - [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.* ## Attribute arguments and parameters