これがメインクラスです
class Program
{
static void Main(string[] args)
{
MyArray fruit = new MyArray(-2, 1);
fruit[-2] = "Apple";
fruit[-1] = "Orange";
fruit[0] = "Banana";
fruit[1] = "Blackcurrant";
Console.WriteLine(fruit[-1]); // Outputs "Orange"
Console.WriteLine(fruit[0]); // Outputs "Banana"
Console.WriteLine(fruit[-1,0]); // Output "O"
Console.ReadLine();
}
}
インデクサーのクラスは次のとおりです。
class MyArray
{
int _lowerBound;
int _upperBound;
string[] _items;
public MyArray(int lowerBound, int upperBound)
{
_lowerBound = lowerBound;
_upperBound = upperBound;
_items = new string[1 + upperBound - lowerBound];
}
public string this[int index]
{
get { return _items[index - _lowerBound]; }
set { _items[index - _lowerBound] = value; }
}
public string this[int word, int position]
{
get { return _items[word - _lowerBound].Substring(position, 1); }
}
}
したがって、「クラスMyArray」で定義されている多次元インデクサ
「word」の値として「-1」をインデクサーに渡すと、これがどのように機能するのか理解できません。'_lowerbound'の値は'-2'です。つまり、returnの値は_items [-1-(-2)]である必要があり、これにより_items[1]になります。
しかし実際には、果物の-1インデックス、つまりオレンジを指しています。
私の疑問を解消してください。