What is the difference between using int and uint? All the examples I have seen so far are using int for integers. Any advantage of using uint? Thanks.
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 .
Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.
A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.
uint and ulong are the unsigned versions of int and long . That means they can't be negative. Instead they have a larger maximum value. To write a literal unsigned int in your source code you can use the suffix u or U for example 123U .
uint
means unsigned int, you can use it for a 0 .. +4G range
where the normal (signed) int has a -2G .. +2G range.
When to use it? Almost never. It is not a CLS compliant type so you should never use it in the public interface of an assembly. Not all .NET languages can handle it.
Their main use is in P/Invoke to unmanaged code and some rare bitmask situations. In .NET, we do most bitmasking with normal signed ints.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With