user366866
user366866

Reputation: 55

Is there way of stopping uint underflow in C#?

Use int to test if below 0 . uint in c# wraps around to a huge number once it goes below 0, this is in all programming languages.

Upvotes: 0

Views: 1136

Answers (2)

Yola
Yola

Reputation: 19043

You can do it like this

uint i = 0;
i = checked(i - 1);

This will throw System.OverflowException. Though it will not stay zero, at least you will be sure that no overflow happened.

Upvotes: 4

user366866
user366866

Reputation: 55

I just use ordinary int instead of uint, and check if its < 0... problem solved

Upvotes: -2

Related Questions