1

From what I understand, the Length property of a C# System.Array returns an integer value according to the following method signature.

public int Length { get; }

My question then is can the value returned, by calling this method on an instance of an array, ever be negative?

From the signature alone I would normally assume it is a possibility since an "int" can take on both positive and negative values. I guess I'm just hoping I can trust that it will always be 0 or greater and not have to perform a check to see if the value is negative before using it to iterate over the array in a loop or something.

I was wondering if this was an error in design or if I was missing some sort of hidden insight since I probably would have went with a "uint" for the return type.

Edit:

Thank you for pointing me to the reasoning behind the design choice for the int vs uint return type, I was happy to learn that it had a valid rationale behind it.

My question was more related to the "possibility" of EVER getting a negative return value, either from misuse, improper initialization, or some unforeseen circumstances like the one I came across in the question here: Negative Array.Length while using Visual Studio 2019

  • 5
    No, it cannot be negative, but there are reasons for which it was a good idea to make Length an `int`. CLS Compliance is the main one, and most commonly cited, but also imagine a reverse for loop: `for (var i = arr.Length - 1; i >= 0; i--)`: If `Length` were unsigned, `i--` would change `i` from `0` to `uint.Max`, and this loop would repeat infinitely. – StriplingWarrior Oct 06 '22 at 22:48
  • @StriplingWarrior, I'll accept the CLS Compliance being a good idea, even though I personally think C# was a bad idea, due to my recently needing to port C++ code, with which I've become somewhat adept, over to C# and feeling like I'm coding with both hands tied behind my back hehe. But the reverse for loop argument probably woudn't have been enough to convince me of the design decision, as people would have figured out solutions: https://stackoverflow.com/questions/275994/whats-the-best-way-to-do-a-backwards-loop-in-c-c-c – Pascal Laferrière Oct 15 '22 at 01:14
  • No it can't be negative however, you can get null pointer exception if array is null. – maddy23285 Oct 15 '22 at 02:00
  • 2
    ​Funnily enough [it is possible](https://github.com/dotnet/runtime/issues/76116 "List.AddRange with erroneous input has inconsistent behavior") to make a `List` with negative `Count` in .NET 6. It has been fixed in the upcoming .NET 7. – Theodor Zoulias Oct 15 '22 at 04:02
  • @PascalLaferrière: My condolences on your need to port C++ code to C#. Of course people would eventually figure out solutions, but the _principle of least surprise_ is strong in this situation. When a seemingly-basic loop yields totally unexpected behavior just because you haven't learned the nuances of a language, well... [language designers pay attention to that sort of thing](https://stackoverflow.com/a/8899347/120955). – StriplingWarrior Oct 16 '22 at 03:52
  • 1
    @TheodorZoulias: That's an awesome find. Well done! – StriplingWarrior Oct 16 '22 at 03:53
  • @StriplingWarrior: Thanks for that additional link, definitely something to pay attention to, and it's situations like that which prompted my question. Since it's production code, I like to make sure I understand as much as possible about even the seemingly trivial things and what to expect in most (i.e. those not maliciously engineered to fail) situations. – Pascal Laferrière Oct 16 '22 at 05:13

0 Answers0