1

It is very, very common to see code like this:

for (int i = 0; i < array.Length; i++)
{
    DoSomething(array[i]);
}

The above code makes certain assumptions about arrays (which apparently hold true most of the time, but not all the time). Wouldn't it be more explicit and more forward-compatible to use something like this instead?

for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
    DoSomething(array[i]);
}

Why is the former format so widely accepted and used?

(I know we could use foreach, but let's assume that there is some reason that would not work for us in this specific case).

7
  • 1
    If you are iterating through the array, in c#, you should be using the foreach construct which is specifically for that purpose. You'll need to describe why that's not available otherwise we are speculating about an alternate universe. – whatsisname Jan 11 '17 at 0:50
  • 1
    foreach is not always appropriate, e.g. if working with parallel arrays, or if adding and removing items from the array. – John Wu Jan 11 '17 at 0:51
  • It is probably convenient to think of arrays as going from 0, 1, 2, ..., N-1. The first version reflects this thought process. Also most languages make this choice already and don't let you change it. Even in C# you could always as a team have a style rule that says not to use the feature that lets you change array bounds. – Brandin Jan 11 '17 at 8:22
  • 20 years ago, i was arguing (from the POV of DSP) with the creator of MATLAB that all MATLAB arrays (or "matrices" or "variables") should have adjustable index origins. if arrays are made into structs (or whatever they call them in C#) with the array bounds tightly attached, then maybe you gotta point. but array indexing should be simply translatable to pointer arithmetic and that is far easier when each dimension of the array has 0 as the origin. also check out this. – robert bristow-johnson Jan 11 '17 at 9:05
1

Your suggested alternative assumes that the array is one dimensional. So, I guess you can't win them all... ;)

Arrays are a relatively primitive data type. I feel free using them internally to the implementation of an abstraction, just as I would using int's and strings. In those cases, the creator of the array and the consumer/user of the array is one and the same class, so the assumptions of zero-based index are natural, easy to verify, and relatively easy to change if there'd be some reason.

I would feel less free using an array as part of an abstraction for a client (even if only me later) to use, just as I feel more uncomfortable making an abstraction in terms of primitive int or string. In the case of creating an abstraction, I'd prefer handing out (or consuming) a dedicated type that represents a collection or an iterator/generator. This to protect both clients and me from the language-specific details of arrays (ints/strings), to create more type safety by introducing well defined types, and to try to move the abstraction up toward the client's domain and away from the implementation domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.