<p>Why is <code>Array.Length</code> an int, and not a <code>uint</code>. This bothers me (just a bit) because a length value can never be negative. </p>

<p>This also forced me to use an int for a length-property on my own class, because when you 
specify an int-value, this needs to be cast explicitly...</p>

<p>So the ultimate question is: is there any use for an unsigned int (<code>uint</code>)? Even Microsoft seems not to use them.</p>

[{'answer_id': 6309, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': -1, 'selected': False, 'text': "<p>Typically, integer values are signed, unless you explicitly need an unsigned value. It's just the way they are used. I may not agree with that choice, but that's just the way it is.</p>\n\n<p>For the time being, with todays typical memory constraints, if your array or similar data structure needs an UInt32 length, you should consider other data structures.</p>\n\n<p>With an array of bytes, Int32 will give you 2GB of values</p>\n"}, {'answer_id': 6320, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 7, 'selected': True, 'text': '<p>Unsigned int isn\'t CLS compliant and would therefore restrict usage of the property to those languages that do implement a <code>UInt</code>. </p>\n\n<p>See here:</p>\n\n<p>Framework 1.1</p>\n\n<blockquote>\n  <p><a href="http://msdn.microsoft.com/en-us/library/hfa3fa08(VS.71).aspx" rel="noreferrer">Introduction to the .NET Framework Class Library</a></p>\n</blockquote>\n\n<p>Framework 2.0</p>\n\n<blockquote>\n  <p><a href="http://msdn.microsoft.com/en-us/library/hfa3fa08(VS.80).aspx" rel="noreferrer">.NET Framework Class Library Overview </a></p>\n</blockquote>\n'}, {'answer_id': 6338, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>I think it also might have to do with simplifying things on a lower level, since Array.Length will of course be added to a negative number at some point, if Array.Length were unsigned, and added to a negative int (two's complement), there could be messy results.</p>\n"}, {'answer_id': 197975, 'author': 'Constantin', 'author_id': 20310, 'author_profile': 'https://Stackoverflow.com/users/20310', 'pm_score': 1, 'selected': False, 'text': '<p>Looks like nobody provided answer to "the ultimate question".</p>\n\n<p>I believe primary use of unsigned ints is to provide easier interfacing with external systems (P/Invoke and the like) and to cover needs of various languages being ported to .NET.</p>\n'}, {'answer_id': 491544, 'author': 'ShuggyCoUk', 'author_id': 12748, 'author_profile': 'https://Stackoverflow.com/users/12748', 'pm_score': 6, 'selected': False, 'text': '<p>Many reasons:</p>\n\n<ul>\n<li>uint is not CLS compliant, thus making a built in type (array) dependent on it would have been problematic</li>\n<li>The runtime as originally designed prohibits any object on the heap occupying more than 2GB of memory. Since the maximum sized array that would less than or equal to this limit would be new byte[int.MaxValue] it would be puzzling to people to be able to generate positive but illegal array lengths.\n\n<ul>\n<li>Note that this limitation has been <a href="http://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx" rel="noreferrer">somewhat removed in the 4.5 release</a>, though the standard Length as int remains.</li>\n</ul></li>\n<li>Historically C# inherits much of its syntax and convention from C and C++. In those arrays are simply pointer arithmetic so negative array indexing was possible (though normally illegal and dangerous). Since much existing code assumes that the array index is signed this would have been a factor</li>\n<li>On a related note the use of signed integers for array indexes in C/C++ means that interop with these languages and unmanaged functions would require the use of ints in those circumstances anyway, which may confuse due to the inconsistency.</li>\n<li>The BinarySearch implementation (a very useful component of many algorithms) relies on being able to use the negative range of the int to indicate that the value was not found <strong>and</strong> the location at which such a value should be inserted to maintain sorting.</li>\n<li>When operating on an array it is likely that you would want to take a negative offset of an existing index. If you used an offset which would take you past the start of the array using unit then the wrap around behaviour would make your index possibly legal (in that it is positive). With an int the result would be illegal (but safe since the runtime would guard against reading invalid memory)</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/6301', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/56/']