Thanks to visit codestin.com
Credit goes to github.com

Skip to content

IndexerNameAttribute should affect reserved member names #1303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KalleOlaviNiemitalo opened this issue Apr 3, 2025 · 1 comment
Open

Comments

@KalleOlaviNiemitalo
Copy link
Contributor

Describe the bug

C# 7 §15.3.10.4 (Member names reserved for indexers) says that specific signatures of get_Item and set_Item are reserved, and that Item is a reserved member name, when the class has an indexer. However, if the indexer has an IndexerNameAttribute, then the name given in that attribute should be reserved instead of Item.

Example

using System.Runtime.CompilerServices;
public class C {
    [IndexerName("Slot")]
    int this[int i] => 0;

    // The standard says that get_Item is a reserved member name,
    // and that declaring it here causes a compile-time error.
    // It should not be reserved in this case.
    int get_Item(int i) => 1;
    
    // Instead, get_Slot should be reserved.
    //int get_Slot(int i) => 2;
}

Expected behavior

The example should not cause a compile-time error.

§15.3.10.4 (Member names reserved for indexers) should say that IndexerNameAttribute has this effect.

Additional context

Inspired by #1302.

IndexerNameAttribute is referenced under §22.5.5.4 (The CallerMemberName attribute):

For invocations that occur within indexer accessors, the member name used is that supplied by an `IndexerNameAttribute` ([§22.6](attributes.md#226-attributes-for-interoperation)) on the indexer member, if present, or the default name `Item` otherwise.

And in §C.2 (Standard Library Types defined in ISO/IEC 23271):

public sealed class IndexerNameAttribute : Attribute
{
public IndexerNameAttribute(String indexerName);
}

@KalleOlaviNiemitalo
Copy link
Contributor Author

Amusingly,

using System.Runtime.CompilerServices;

class C1 {
    // is allowed by a compiler
    [IndexerName("C1")]
    int this[int i] => 0;
}

class C2 {  
    // error CS0542: 'C2': member names cannot be the same as their enclosing type
    int C2 => 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant