Sometimes a property is helpful, if only as syntactic sugar, within the
class too. Just because you can access the internal state directly doesn't
mean that it's always the nicest way. For instance:
class JaggedArray<T> {
T[][] manyTs;
foo(int[] cols, int rows) {
manyTs = new T[rows];
int colsIndex = 0;
foreach (T[] S in manyTs) {
S = new T[cols[colsIndex++]];
}
}
public T this[int col, int row] {
get {
if(row < manyTs.Count &&
col < manyTs[row].Count) {
return manyTs[row][col];
} else {
throw System.IndexOutOfRangeException;
}
}
set {
if(row < manyTs.Count &&
col < manyTs[row].Count) {
manyTs[row][col] = value;
} else {
throw System.IndexOutOfRangeException;
}
}
}
}
Note that the get and set accessors are somewhat complex. Even within the
class, say in an iterator or something, you might well like to be able to
use the 'this[col,row]' notation to access an individual element in the
jagged array rather than repeating the index-checking logic every time you
needed to access an element. If, for some reason, you needed your
JaggedArray class to be read only then, as the original poster indicated,
you might well want different protection levels for 'get' and 'set'.