如何制作锯齿状索引器

时间:2013-10-18 04:15:45

标签: c# indexer

我在某处看到了锯齿状的索引器,并想知道如何让它们工作。

我知道我可以做到以下几点:

class foo
{
    public string this[int i1]
    {
        get{ return GetString(i1); }
    }

    public string this[int i1, int i2]
    {
        get{ return GetString(i1) + GetString(i2); }
    }
}

那样:

string s1 = foo[5];
string s2 = foo[12,8];

问题是,如何定义索引器...

string s2 = foo[12][8];

如果可能(以及其他方面不清楚),我也会很感激设定者的定义。

foo[12][8] = "qwerty";

1 个答案:

答案 0 :(得分:4)

Derrick Shepard的回答似乎是正确的,但是我有一些笔记给你:

使用您当前的方法:

public string this[int i1]
public string this[int i1, int i2]

foo[12][8]将等同于(foo[12])[8]解析;你将获得string foo[12],然后获得第9个角色。

如果您愿意更改第一个方法(带有单个参数的索引器),您可以考虑返回一个对象,而该对象又提供另一个索引器。

相关问题