In C# should I use uint or int for values that are never supposed to be negative?
Posted
by Hamish Grubijan
on Stack Overflow
See other posts from Stack Overflow
or by Hamish Grubijan
Published on 2010-06-03T02:54:56Z
Indexed on
2010/06/03
3:04 UTC
Read the original article
Hit count: 350
Suppose that the MaxValue
of (roughly :) ) 2^31 vs 2^32 does not matter.
On one hand, using uint
seems nice because it is self-explanatory, it indicates (and promises?) that some value may never be negative. However, int
is more common, and a cast is often inconvenient. One can just use int and always supplement it with code contracts (everyone has moved to .Net 4.0 by now, right?) Standard libraries do use int
for Length
and Size
properties, even though those should never be negative. So, is it obvious to you that int
is better than uint
most of the time, or is it more complicated?
Please ask questions if you find that this question is not clearly stated.
Thanks.
© Stack Overflow or respective owner