tags:

views:

659

answers:

6

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

Hi,

I always wonder why Count isn't an unsigned integer instead of a signed one?

For example, take ListView.SelectedItems.Count. The number of elements can't be less then 0, so why is it a signed int?

If I try to test if there are elements selected, I would like to test

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

but because it's a signed integer, I have to test

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

or is there any case when .Count could be < 0 ?

+40  A: 

Unsigned integer is not CLS-compliant (Common Language Specification)

For more info on CLS compliant code, see this link:

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

Philippe Leybaert
Although this answers the question to the letter, it doesn’t explain. Perhaps the question should rather have been, “*why* isn’t it CLS compliant?” That was indeed a weird decision.
Konrad Rudolph
The question why uint is not CLS-compliant is a topic for a separate question, which has already been asked here: http://stackoverflow.com/questions/6325/why-are-unsigned-ints-not-cls-compliant
Philippe Leybaert
A: 

Practically, can you select less than 0 items without upsetting the time-space continuum?

Jonathan
Does this fall in the same domain as parsing HTML with regular expressions?
Zoidberg
No. That's why the question is asked.
Kobi
Sure, you can. There may be a rule that whenever "X" appears in a list, you remove it from the selection. So, you basically have a -1 modifier to your select list... ;o)
John Fisher
+3  A: 

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

Here is very similar thread about arrays:

http://stackoverflow.com/questions/6301/why-is-array-length-an-int-and-not-an-uint

Andrzej Nosal
It is part of the CLR. Just not of the CLS
Henk Holterman
That's right, I've corrected it
Andrzej Nosal
+2  A: 

Let’s look at this from a practical angle.

For better or worth, singed ints are the normal sort of ints on use on .net, it was also normal to use singed ints in C and C++. So most variables are declared to be “int” rather than “unsigned int” unless there is a good reason otherwise.

Converting between an unsigned int and a signed int has issues and is not always safe.

On a 32 bit system it is not possible for a collection to have anywhere close to 2^^32 items in it, so a signed int is big enough in all cases.

On a 64 bit system, anunsigned int does not gain you much, in most cases a singed int is still big enough, otherwise you need to use a 64 bit int. (I expect that done of the standard collection will cope well with anywhere near 2^^31 items on a 64 system!)

Therefore given that using an unsigned int has no clear advantage, so why would you use a unsigned int?

Ian Ringrose
+1  A: 
  1. It's not CLS compliant, largely to allow wider support from different languages.

  2. A signed int offers ease in porting code from C or C++ that uses pointer arithmetic.

  3. Count can be part of an expression where the overall value can be negative. In particular, count has a direct relationship to indices, where valid indices are always in the range [0, Count - 1], but negative results are used e.g. by some binary search methods (including those provided by the BCL) to reflect the position where a new item should be inserted to maintain order.

Jon Hanna
A: 

In vb.net, the normal looping construct (a "For/Next loop") will execute the loop with values up to and including the maximum value specified, unlike C which can easily loop with values below the upper limit. Thus, it is often necessary to specify a loop as e.g. "For I=0 To Array.Length-1"; if Array.Length were unsigned and and zero, that could cause an obvious problem. Even in C, one benefits from being able to say "for (i=Array.Length-1; i GE 0; --i)". Sometimes I think it would be useful to have a 31-bit integer type which would support widening casts to both signed and unsigned int, but I've never heard of a language supporting such.

supercat