Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Array.Length an int, and not an uint [duplicate]

Tags:

c#

.net

int

uint

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

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...

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

like image 696
doekman Avatar asked Aug 08 '08 19:08

doekman


People also ask

What is the difference between an int and a Uint?

uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero). In addition there two alias types: byte which is the same as uint8 and rune which is the same as int32 .

Is Uint smaller than int?

int is shorter to type than uint .

How do you find the length of an array?

Using sizeof() function to Find Array Length in C++ Hence, if we simply divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.

What is the difference between long and Ulong?

A long value is stored in 64-bit,with its first digit to show if it's a positive/negative number. while ulong is also 64-bit, with all 64 bit to store the number. so the maximum of ulong is 2(64)-1, while long is 2(63)-1. Save this answer.


2 Answers

Unsigned int isn't CLS compliant and would therefore restrict usage of the property to those languages that do implement a UInt.

See here:

Framework 1.1

Introduction to the .NET Framework Class Library

Framework 2.0

.NET Framework Class Library Overview

like image 180
Kev Avatar answered Sep 23 '22 16:09

Kev


Many reasons:

  • uint is not CLS compliant, thus making a built in type (array) dependent on it would have been problematic
  • 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.
    • Note that this limitation has been somewhat removed in the 4.5 release, though the standard Length as int remains.
  • 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
  • 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.
  • 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 and the location at which such a value should be inserted to maintain sorting.
  • 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)
like image 30
ShuggyCoUk Avatar answered Sep 23 '22 16:09

ShuggyCoUk