Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .openpublishing.redirection.csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1502.md",
"redirect_url": "/dotnet/csharp/misc/cs1503"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1546.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/indexer-access-errors"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1564.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
Expand Down Expand Up @@ -666,6 +670,10 @@
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8418.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-statement-declaration-errors#implementing-idisposable-and-iasyncdisposable"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8422.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/local-function-errors"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8515.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/pattern-matching-warnings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ f1_keywords:
- "CS7046"
- "CS7047"
- "CS7067"
- "CS8423"
- "CS8783"
- "CS8959"
- "CS8960"
- "CS8961"
- "CS8962"
- "CS8963"
- "CS8968"
- "CS8783"
- "CS8970"
- "CS9331"
helpviewer_keywords:
Expand Down Expand Up @@ -84,16 +85,17 @@ helpviewer_keywords:
- "CS7046"
- "CS7047"
- "CS7067"
- "CS8423"
- "CS8783"
- "CS8959"
- "CS8960"
- "CS8961"
- "CS8962"
- "CS8963"
- "CS8968"
- "CS8783"
- "CS8970"
- "CS9331"
ms.date: 05/19/2026
ms.date: 07/16/2026
ai-usage: ai-assisted
---
# Resolve errors and warnings related to attribute declarations or attribute use in your code
Expand Down Expand Up @@ -139,13 +141,14 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**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.*
- [**CS8423**](#attribute-location-context): *Attribute 'attribute' is not valid on event accessors. It is only valid on 'declaration' declarations.*
- [**CS8783**](#conditional-attribute-usage): *Local function 'method' must be 'static' in order to use the Conditional attribute*
- [**CS8959**](#callerargumentexpression-attribute-usage): *CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type1 to type2*
- [**CS8960**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is overridden by the CallerLineNumberAttribute.*
- [**CS8961**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is overridden by the CallerFilePathAttribute.*
- [**CS8962**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is overridden by the CallerMemberNameAttribute.*
- [**CS8963**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is applied with an invalid parameter name.*
- [**CS8968**](#attribute-arguments-and-parameters): *An attribute type argument cannot use type parameters*
- [**CS8783**](#conditional-attribute-usage): *Local function 'method' must be 'static' in order to use the Conditional attribute*
- [**CS8970**](#attribute-arguments-and-parameters): *Type cannot be used in this context because it cannot be represented in metadata.*
- [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.*

Expand Down Expand Up @@ -210,6 +213,7 @@ The following errors occur when you apply attributes in invalid locations or use
- **CS0658**: *Location is not a recognized attribute location. Valid attribute locations for this declaration are listed. All attributes in this block will be ignored.*
- **CS1667**: *Attribute is not valid on property or event accessors. It is only valid on specific declarations.*
- **CS7014**: *Attributes are not valid in this context.*
- **CS8423**: *Attribute 'attribute' is not valid on event accessors. It is only valid on 'declaration' declarations.*

To correct these errors, follow these rules. For more information, see [Attribute Targets](../../advanced-topics/reflection-and-attributes/index.md#attribute-targets) and the [C# language specification section on attribute specification](~/_csharpstandard/standard/attributes.md#233-attribute-specification).

Expand All @@ -218,6 +222,7 @@ To correct these errors, follow these rules. For more information, see [Attribut
- The attribute target specifier you used isn't a recognized specifier (**CS0658**). Valid specifiers include `assembly:`, `module:`, `type:`, `method:`, `property:`, `field:`, `event:`, `param:`, and `return:`.
- Some attributes, such as <xref:System.ObsoleteAttribute> and <xref:System.CLSCompliantAttribute>, aren't valid on property or event accessors (**CS1667**). Move the attribute from the accessor to the property or event declaration itself.
- Attributes can only appear on program elements that support them (**CS7014**). If you're applying assembly-level or module-level attributes, use the `assembly:` or `module:` target specifiers and place them at the top of the file.
- Attributes on event `add` and `remove` accessors aren't allowed (**CS8423**). Apply the attribute to the event declaration, or use a valid target such as `method:` or `param:` on a declaration that supports that target.

## Predefined attributes

Expand Down
59 changes: 0 additions & 59 deletions docs/csharp/language-reference/compiler-messages/cs1546.md

This file was deleted.

78 changes: 0 additions & 78 deletions docs/csharp/language-reference/compiler-messages/cs8422.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: "Resolve errors and warnings related to indexer and element access"
description: "This article helps you diagnose and correct compiler errors and warnings related to indexer and element access"
f1_keywords:
- "CS0021"
- "CS0154"
- "CS0271"
- "CS0272"
- "CS0856"
- "CS0857"
- "CS1545"
- "CS1546"
- "CS8428"
- "CS8429"
helpviewer_keywords:
- "CS0021"
- "CS0154"
- "CS0271"
- "CS0272"
- "CS0856"
- "CS0857"
- "CS1545"
- "CS1546"
- "CS8428"
- "CS8429"
ms.date: 07/16/2026
ai-usage: ai-assisted
---
# Resolve errors and warnings related to indexer and element access

This article covers the following compiler errors and warnings:

<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
-->
- [**CS0021**](#indexing-non-indexable-types): *Cannot apply indexing with [] to an expression of type 'type'*
- [**CS0154**](#accessor-availability-and-accessibility): *The property or indexer 'indexer' cannot be used in this context because it lacks the get accessor*
- [**CS0271**](#accessor-availability-and-accessibility): *The property or indexer 'indexer' cannot be used in this context because the get accessor is inaccessible*
- [**CS0272**](#accessor-availability-and-accessibility): *The property or indexer 'indexer' cannot be used in this context because the set accessor is inaccessible*
- [**CS0856**](#indexed-properties-from-com-and-interop): *Indexed property 'indexer' has non-optional arguments which must be provided*
- [**CS0857**](#indexed-properties-from-com-and-interop): *Indexed property 'indexer' must have all arguments optional*
- [**CS1545**](#indexed-properties-from-com-and-interop): *Property, indexer, or event 'member' is not supported by the language; try directly calling accessor methods 'method' or 'method'*
- [**CS1546**](#indexed-properties-from-com-and-interop): *Property, indexer, or event 'member' is not supported by the language; try directly calling accessor method 'method'*
- [**CS8428**](#implicit-index-and-range-indexers): *Invocation of implicit Index Indexer cannot name the argument.*
- [**CS8429**](#implicit-index-and-range-indexers): *Invocation of implicit Range Indexer cannot name the argument.*

These diagnostics cover element access expressions such as `value[index]`, explicit indexers, indexed properties imported from metadata or COM interop, and the implicit `Index` and `Range` indexers used by the `^` and `..` operators.

## Indexing non-indexable types

- **CS0021**: *Cannot apply indexing with [] to an expression of type 'type'*

Use `[]` only with arrays, pointer types, or types that define an applicable indexer. If the type should support element access, add an indexer declaration to the type. Otherwise, call the member that retrieves the element, or convert the value to a type that supports indexing before using `[]`.

## Accessor availability and accessibility

- **CS0154**: *The property or indexer 'indexer' cannot be used in this context because it lacks the get accessor*
- **CS0271**: *The property or indexer 'indexer' cannot be used in this context because the get accessor is inaccessible*
- **CS0272**: *The property or indexer 'indexer' cannot be used in this context because the set accessor is inaccessible*

Reading an indexer requires an accessible `get` accessor. Writing an indexer requires an accessible `set` or `init` accessor. Add the missing accessor, change the accessor accessibility, or use the indexer only in contexts where the required accessor is available.

## Indexed properties from COM and interop

- **CS0856**: *Indexed property 'indexer' has non-optional arguments which must be provided*
- **CS0857**: *Indexed property 'indexer' must have all arguments optional*
- **CS1545**: *Property, indexer, or event 'member' is not supported by the language; try directly calling accessor methods 'method' or 'method'*
- **CS1546**: *Property, indexer, or event 'member' is not supported by the language; try directly calling accessor method 'method'*

Some metadata patterns, especially from COM or other .NET languages, expose indexed properties that C# can't bind with ordinary property or indexer syntax. Supply all required indexed property arguments when the metadata requires them (**CS0856**). When C# requires indexed property arguments to be optional, change the imported member or wrapper so all arguments are optional (**CS0857**).

For unsupported imported properties, indexers, or events, call the generated accessor method directly from C# source when the accessor itself is callable (**CS1545**, **CS1546**). For example, call `get_Prop(arguments)` or `set_Prop(arguments, value)` instead of using property syntax when the member can't be represented directly in C#.

## Implicit index and range indexers

- **CS8428**: *Invocation of implicit Index Indexer cannot name the argument.*
- **CS8429**: *Invocation of implicit Range Indexer cannot name the argument.*

The compiler can synthesize an indexer for the [index from end (`^`) and range (`..`) operators](../operators/member-access-operators.md#index-from-end-operator-) when a type has the required `Length` or `Count` member, an `int` indexer, or a compatible `Slice` method. Calls to those implicit `Index` or `Range` indexers must use positional arguments. Remove the argument name from the invocation and pass the `Index` or `Range` value positionally (**CS8428**, **CS8429**).

## Related diagnostics

- For more information about array indexing and rank, see [Resolve errors and warnings related to array and collection declarations and initializations](array-declaration-errors.md) in C# compiler messages.
- For more information about argument lists, see [Errors and warnings for parameter / argument mismatches](parameter-argument-mismatch.md) in C# compiler messages.
- For more information about overload resolution for indexers and element access, see [Fix errors that involve overload resolution](overload-resolution.md) in C# compiler messages.
- For more information about read-only and init-only properties, see [Compiler Errors on property declarations](property-declaration-errors.md) in C# compiler messages.
- For more information about dynamic binding, see [Resolve errors related to dynamic binding and the dynamic type](dynamic-type-and-binding-errors.md) in C# compiler messages.
- For more information about indexer access in expression trees, see [Some expressions are prohibited in expression trees](expression-tree-restrictions.md) in C# compiler messages.
- For more information about inline array element access, see [Resolve errors related to inline arrays](inline-array-errors.md) in C# compiler messages.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ f1_keywords:
- "CS0736"
- "CS0737"
- "CS0738"
- "CS8646"
- "CS8705"
- "CS8854"
- "CS9333"
Expand All @@ -47,11 +48,12 @@ helpviewer_keywords:
- "CS0736"
- "CS0737"
- "CS0738"
- "CS8646"
- "CS8705"
- "CS8854"
- "CS9333"
- "CS9334"
ms.date: 11/12/2025
ms.date: 07/16/2026
ai-usage: ai-assisted
---
# Resolve errors and warnings related to members that implement an interface
Expand Down Expand Up @@ -81,6 +83,7 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS0736**](#method-visibility-and-modifiers): *Member does not implement instance interface member. It cannot implement the interface member because it is static.*
- [**CS0737**](#method-visibility-and-modifiers): *Member does not implement interface member. It cannot implement an interface member because it is not public.*
- [**CS0738**](#return-types-and-signatures): *Member does not implement interface member. It cannot because it does not have the matching return type.*
- [**CS8646**](#ambiguous-and-conflicting-implementations): *'member' is explicitly implemented more than once.*
- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member does not have a most specific implementation. Neither member is most specific.*
- [**CS8854**](#return-types-and-signatures): *Member does not implement interface member.*
- [**CS9333**](#return-types-and-signatures): *Parameter type must match implemented member.*
Expand Down Expand Up @@ -205,11 +208,13 @@ For more information, see [Interfaces](../../fundamentals/types/interfaces.md),
The following errors occur when the compiler can't determine which interface implementation to use:

- **CS0473**: *Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead.*
- **CS8646**: *'member' is explicitly implemented more than once.*
- **CS8705**: *Interface member 'member' does not have a most specific implementation. Neither is most specific.*

You can correct these errors using the following techniques:

- Eliminate the explicit interface implementation and instead use a single implicit public implementation for both interface methods (**CS0473**). When a generic method acquires the same signature as a non-generic method (such as when implementing `ITest<int>` where both `TestMethod(int)` and `TestMethod(T)` become identical), the common language infrastructure metadata system can't unambiguously determine which interface member binds to which implementation slot, so using implicit implementation allows the single method to satisfy both interface requirements.
- Remove the duplicate explicit interface implementation for the same interface member (**CS8646**). A type can explicitly implement an interface member only once, so keep the implementation that should satisfy the interface contract and delete the duplicate declaration.
- Provide an explicit implementation in the implementing class or struct that resolves the ambiguity between multiple default implementations (**CS8705**). This error typically occurs with diamond inheritance patterns where a class implements multiple interfaces that each provide default implementations for the same member. The compiler needs you to explicitly specify which implementation to use, or provide your own implementation.
- Restructure the interface hierarchy to avoid diamond inheritance conflicts where multiple interfaces provide default implementations for the same member (**CS8705**). By redesigning the interface relationships or consolidating the default implementations into a single interface, you can eliminate the ambiguity that prevents the compiler from determining the most specific implementation.

Expand Down
Loading
Loading