Search code examples
c#.netuint

Why don't I get an exception on integer (uint) under/overflow?


I accidentally discovered this today. Is this supposed to happen? Is this something we should all be warned about?

Since this is an unsigned integer, shouldn't I have gotten an error?

uint foo = 10;
foo = foo - 35;
Console.WriteLine(foo);

Output: 4294967271


Solution

  • you have an arthmetic overflow just enable Buid-Advanced-Check for arithmetic... that and you will get this exception:

    An unhandled exception of type 'System.OverflowException' occurred in Additional information: Arithmetic operation resulted in an overflow.