Class with indexer and property named “Item”

依然范特西╮ 提交于 2019-11-26 20:45:35

Based on this site, it is possible to use an attribute to rename the Indexer

public class MyClass
{
    public object Item { get; set; }
    [System.Runtime.CompilerServices.IndexerName("MyItem")]
    public object this[string index] { get { return null; } set { } }
}

C# internally creates a property called Item for languages that don't support the indexer. You can control this name using the IndexerNameAttribute, like this:

[IndexerName("MyIndexer")]
public object this[string index]
{
    get { return blah; }
}

If I remember correctly, such an indexer can be accessed from VB.Net through an "Item()" method. That would be where that "defined twice" comes from.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!