I want to convert long to int.
long
int
If the value of long > int.MaxValue, I am happy to let it wrap around.
int.MaxValue
What is the best way?
Just do (int)myLongValue. It'll do exactly what you want (discarding MSBs and taking LSBs) in unchecked context (which is the compiler default). It'll throw OverflowException in checked context if the value doesn't fit in an int:
(int)myLongValue
unchecked
OverflowException
checked
int myIntValue = unchecked((int)myLongValue);
1.4m articles
1.4m replys
5 comments
57.0k users