member names cannot be the same as their enclosing type with partial class

前端 未结 1 582
囚心锁ツ
囚心锁ツ 2021-01-19 02:09

I have defined a partial class with a property like this:

public partial class Item{    
    public string this[string key]
    {
        get
        {
              


        
相关标签:
1条回答
  • 2021-01-19 02:48

    Indexers automatically have a default name of Item - which is the name of your containing class. As far as the CLR is concerned, an indexer is just a property with parameters, and you can't declare a property, method etc with the same name as the containing class.

    One option is to rename your class so it's not called Item. Another would be to change the name of the "property" used for the indexer, via [IndexerNameAttribute].

    Shorter example of brokenness:

    class Item
    {
        public int this[int x] { get { return 0; } }
    }
    

    Fixed by change of name:

    class Wibble
    {
        public int this[int x] { get { return 0; } }
    }
    

    Or by attribute:

    using System.Runtime.CompilerServices;
    
    class Item
    {
        [IndexerName("Bob")]
        public int this[int x] { get { return 0; } }
    }
    
    0 讨论(0)
提交回复
热议问题