0

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

8
  • 1
    uint means unsigned int Commented Jun 6, 2016 at 13:11
  • I was expecting to get an error - shouldn't I have gotten an error? Commented Jun 6, 2016 at 13:11
  • 4
    Is this something we should all be warned about? It should be understood that by default, C# uses an unchecked context. That allows wrapping like this to happen. If you want an error/warning, change your project configuration to use a checked context by default, or use the checked keyword. Commented Jun 6, 2016 at 13:12
  • 5
    Yes, you should be warned about it. Don't hesitate to enable that warning. Project > Properties > Build tab > Advanced button > tick the "Check for arithmetic overflow/underflow" checkbox. Commented Jun 6, 2016 at 13:17
  • When asking a question about something that you don't expect to happen, tell what you do expect to happen in your question. Also, show your research. And no, the last thing to point at should generally be your code, not the language or framework you're using, so I removed the ".NET BUG?" from your title if you don't mind. Commented Jun 6, 2016 at 13:20

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.