戢翔 2015-04-02 15:05 采纳率: 0%
浏览 2239
已采纳

C#泛型的下标扩展的写法

如题,比如List buf=new List(new string[3]{"a","b","c"});
正常使用下标:string temp=buf[0];得出temp="a".
现在想写一个扩展:string temp=buf["a"];得出temp="a".
当然,举得列子可能不合适,目的是想写一个下标的那种扩展,我记得有这种扩展,但是忘记不知道怎么写的了,望高手指点

  • 写回答

4条回答 默认 最新

  • threenewbee 2015-04-02 15:14
    关注

    不可以,扩展方法不支持索引器。你只能用自定义的类实现:
    class MyList : List
    {
    public string this[string index]
    {
    return this.First(x => x == index);
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?