11 Vote

C#/.NET: Difference between int and uint

Question by Guest | 2016-11-02 at 18:18

I know about the difference between the data types short, int and long (16-bit, 32-bit and 64-bit integer numbers) in C#. However, recently I have recognized some other integer types in an example code: ushort, uint and ulong - so that seems to be the normal types with an additional prepended u.

What are those types supposed to be? I have played a bit with them and they seem to work like usual integers. So, where is the difference?

ReplyPositiveNegative
Avatar
1Best Answer1 Vote

The "u" is standing for "unsigned". So, those types are integers that you do not have a sign and therefore can only hold positive values.

The advantage of those unsigned integers is that they can accept much higher numbers at the same bit length.

Here you can see the minimum and maximum values of the types:

TypeBitsMinMax
sbyte8-128127
byte80255
short16-32.76832.767
ushort16065.535
int32-2.147.483.648-2.147.483.647
uint3204.294.967.295
long64-9.223.372.036.854.775.8089.223.372.036.854.775.807
ulong64018.446.744.073.709.551.615

As you can see, at the same bit length, the unsigned integers can take twice as large values. You should always use such types in a case in which you know that a value will never become negative but can become very large (and therefore can reach the limits of the available range).
2016-11-03 at 16:03

ReplyPositive Negative
Reply

Related Topics

Java: Declare unsigned Integer

Question | 2 Answers

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.