Bruce Wood said:
Tell me more. How does bounds checking come into it?
In certain cases (which I don't believe are applicable here, as the
value of the property could easily change between iterations) the
compiler can remove a lot of bounds checking. Say you have:
string[] x = ...;
for (int i=0; i < x.Length; i++)
{
string y = x
;
///
}
The array access x would normally need to go through a bounds check,
to make sure you're not doing x[-1] or x[1000000]. With the above
(common) code, the JIT compiler can spot that there's no way i is ever
going to be outside the bounds of the array, so can eliminate the
bounds check. I suspect this requires that:
1) x is a local variable, or perhaps a readonly variable.
(Otherwise the value could change between the comparison with x.Length
and the array access.)
2) i and x aren't modified within the body of the loop