Lloyd Dupont <(E-Mail Removed)> wrote:
> just curiosity because I guess there isn't much I could do about it....
>
> if I write
> byte[] buf = ...;
> int N = ...;
> for(int i=0; i<N; i++)
> do(buf[i]);
>
> will the code do a bound checking on the array on every single access to
> buf[i] or will it recognises that N, and buf.Length are constant and
> optimize to only one bounds checking?
It's less likely to do it with the code above than if you do:
for (int i=0; i < buf.Length; i++)
{
do (buf[i]);
}
However, an even better way would be:
foreach (int x in buf)
{
do (x);
}
Readability is almost always far more important than micro-optimisation
like this anyway.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too