Why is Count not an unsigned integer?

Possible duplicates:
Why does .NET use int instead of uint in certain classes?
Why Array.Length is int, not uint

I always wonder why .Count not an unsigned integer instead of a signed one?

For example, take ListView.SelectedItems.Count . The number of elements cannot be less than 0, so why is it a signed int?

If I try to check if there are any selected items, I would like to test

  if (ListView.SelectedItems.Count == 0) {} 

but since this is a signed integer, I need to check

  if (ListView.SelectedItems.Count <= 0) {} 

or is there ever when .Count can be <0?

+43
c #
Sep 07 '10 at 12:15
source share
5 answers

An unsigned integer is not CLS compatible ( Common Language Specification )

For more information on CLS-compatible code, see this link:

http://msdn.microsoft.com/en-us/library/bhc3fa7f.aspx

+51
Sep 07 '10 at 12:19
source share

Let's look at it from a practical angle.

For better or worse, a signed int is the regular int type used in .NET. It was also fine to use signed int in C and C ++. Thus, most variables are declared int , not unsigned int , unless there is a good reason.

Converting between unsigned int and signed int has problems and is not always safe.

On a 32-bit system, a collection cannot contain about 2 ^^ 32 elements , so the signed int is large enough in all cases .. p>

On a 64-bit system, you don’t have much unsigned int , and in most cases the signed int is still large enough, otherwise you need to use the 64-bit int . (I expect that no standard collection will cope well with any objects around 2 ^^ 31 in system 64!)

Therefore, given that using unsigned int does not have a clear advantage, why are you using unsigned int ?

+5
Sep 07 '10 at 13:24
source share

Mabye, because the uint data type is not part of the CLS (common language specification), because not all .NET languages ​​support it.

Here is a very similar thread about arrays:

Why Array.Length is int, not uint

+4
07 Sep '10 at 12:19
source share
  • It is not compatible with CLS, mainly to provide more support from different languages.

  • A signed int makes it easy to port code from C or C ++ that uses pointer arithmetic.

  • Count can be part of an expression where the total value can be negative. In particular, the counter has a direct relationship with the indices, where real indices are always in the range [0, Count-1], but negative results are used, for example. some binary search methods (including those provided by BCL) to reflect the position at which a new element should be inserted to maintain order.

+2
Sep 07 '10 at 13:40
source share

In vb.net, a normal looping construct (a "For / Next loop") will loop with values ​​up to and including the specified maximum value, unlike C, which can easily be short-circuited with values ​​below the upper limit. Thus, it is often necessary to specify a loop, for example. "For i = 0K Array.Length-1"; if Array.Length were unsigned and null, this could cause an obvious problem. Even in C, you can get the opportunity to say "for (i = Array.Length-1; I GE 0; -i)". Sometimes it seems to me that it would be useful to have a 31-bit integer type that supports cast expansion for both signed and unsigned ints, but I never heard of a language that supports such.

0
Sep 07 '10 at 16:24
source share



All Articles