Exercises in this lecture   previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 6.3
Interval indexer

The Interval type represents an oriented interval [from - to] of integers. We use the Interval example to illustrate the overloading of operators. If you have not already done so, read about the idea behind the struct Interval in the course teaching material.

In the client of struct Interval we use an indexer to access elements of the interval. For some interval i, the expression i[0] should access the from-value of i, and i[i.Length-1] should access the to-value of i.

Where, precisely, is the indexer used in the given client class?

Add the indexer to the struct Interval (getter only) which accesses element number j (0 <= j <= i.Length) of an interval i.

Hint: Be careful to take the orientation of the interval into account.

Does it make sense to program a setter of this indexer?