I have two variables.
int x = 1;
uint y = 2;
The types must remain int
and uint
. Yet I can't perform basic arithmetic with them.
y += x;
Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)
So I have to do something like this everytime?
if (x < 0)
{
y -= (uint)Math.Abs(x);
}
else
{
y += (uint)x;
}
Coming from a C/C++/Java background, this confuses me.
Is there something I'm missing here?
Why is this behavior different from the other languages mentioned?
Are there better alternatives?
I assume this is a common trip-up to newcomers of the language.
No, I'm not relying on underflow.