What’s up with this, anyway? I do a simple multiplication:
Int64 x = 11111111111;
Int64 y = 11111111111;
Int64 z = x * y;
And at the end of the multiplication, z shows a value of:
-5670418394979206991
This has clearly overflowed, but no exception is raised. I’d like one to be raised, but…
Note that this is on Windows Phone 7, but I don’t think this has any bearing on the issue. Or does it?
You can use a checked block as pointed out already by other answers:
Which results in an exception:
If you are just doing a single operation where you need checking you can use a checked expression instead:
You can also set the /checked compiler option to have the default be checked.
If you change the default to checked you can use unchecked blocks or expressions to get the unchecked behaviour again.