Why is Count not an unsigned integer?

C#.Net

C# Problem Overview


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

I've 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 ?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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:

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

Solution 3 - C#

Let’s look at this from a practical angle.

For better or worse, signed ints are the normal sort of ints in use in .NET. It was also normal to use signed 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, an unsigned int does not gain you much, in most cases a signed int is still big enough, otherwise you need to use a 64 bit int. (I expect that none 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, why would you use an unsigned int?

Solution 4 - C#

  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.

Solution 5 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInnoView Question on Stackoverflow
Solution 1 - C#Philippe LeybaertView Answer on Stackoverflow
Solution 2 - C#nanView Answer on Stackoverflow
Solution 3 - C#Ian RingroseView Answer on Stackoverflow
Solution 4 - C#Jon HannaView Answer on Stackoverflow
Solution 5 - C#supercatView Answer on Stackoverflow