.NET is an endless supply of fascinating puzzles

By: on September 19, 2007

In C, size_t is unsigned. In Java, there are no unsigned fixed-width pseudointegral types, so it can perhaps be forgiven for having an array’s length field be signed. In .NET, however, which has unsigned ints, an array’s length field is also signed. What could it possibly mean to have a length less than zero?



  1. tonyg says:

    Well that certainly sounds complex.

  2. Alex Blewitt says:

    It’s so that there’s backward compatibility for all those buffer underflows that viruses have come to rely on

  3. matthew says:

    Hang on, if you follow this route too far then you’ll end up wanting different sizes of ints depending on the amount of memory available at runtime – after all, if you’ve only got 32MB of RAM available then the type of the number should not allow values greater than 33554432. But this should also take into account the size of the values in the array – if they’re just bools and they’re bit-packed, then 33554432 won’t be too bad. But if they’re large objects, then a limit rather lower is needed…

  4. tonyg says:

    Matthew, that’s an excellent point! If I were being serious, I’d say something along the lines of “clearly choosing a fixed representation for array lengths (and in fact arrays) is a case of premature optimisation”… but since I’m not, I’ll suggest that what C# really needs is a dependent type system.

  5. Paul Crowley says:

    Being maximally generous I can only imagine something like this:

    o = new objectCache();

    o.maxArraySize = -1; // Disallow all array creation

  6. tonyg says:

    Paul, you are indeed too kind.

  7. Rik Rose says:

    There’s several views this could take, on first reading. Not knowing C# yet, I must confess that I don’t know the actual answer.

    Given a negative index, there are two sensible things that I can see the langauge doing.

    Firstly, it could perform a bounds check on 0 <= index <= sizeof(array), and throwing an error if it’s out of bounds.

    Secondly, it could perform a bounds check on abs(index) <= sizeof(array), and then work backwards from the end, if index < 0. This is what Ruby does.

    I would hazard a guess that option 2 would not have been seen to be as helpful as possible, and so option 1 is what I would expect to happen.

  8. tonyg says:

    Rik, I’m not talking about indexes – I’m talking about the array size itself. Also, the bounds check would be 0 <= index < sizeof(array), rather than 0 <= index <= sizeof(array). An array of size zero is empty, and indexing it at all is an error.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>