If you donβt have the established mode , just throwing it will handle the overflow, as you would expect:
int i = (int)val;
Or, as Matthew Watson suggested, make it independent of the compiler:
int i = unchecked((int)val);
This will not raise an exception. The integer will overflow and continue counting from int.MinValue .
Evidence:
long val = (long)int.MaxValue + 1; int result = (int)val; Console.WriteLine(result == int.MinValue);
source share