不只是可以根据int类型来索引,你也可以根据string类型来索引,例如
class arr
{
private string[] myarr;
public arr(int size)
{
myarr = new string[size];
for (int i = 0; i < size; i++)
{
myarr[i] = "空值";
}
}
public string this[int j]
{
get { return myarr[j]; }
set { myarr[j] = value; }
}
public int getValue(string value)
{
int i = 0;
foreach (string s in myarr)
{
if (s == value)
{
return i;
}
i++;
}
return -1;
}
public int this[string s]
{
get { return getValue(s); }
}
}
|