Difference of two 'uint'

Posted by vanslly on Stack Overflow See other posts from Stack Overflow or by vanslly
Published on 2008-10-26T21:28:23Z Indexed on 2010/05/26 15:31 UTC
Read the original article Hit count: 312

Filed under:
|

When you attempt to declare an unsigned variable in C#.NET with a value outside its value range it is flagged as a compiler error, but if you produce a negative value at runtime and assign it to that variable at runtime the value wraps.

uint z = -1; // Will not compile

uint a = 5;
uint b = 6;
uint c = a - b; // Will result in uint.MaxValue

Is there a good reason why unsigned variables wrap in such a situation instead of throwing an exception?

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET