This is because the calculations on the right side of the assignment are performed in an integer type. And this overwhelming whole
You can fix this with:
public const long MAXIMUM_RANGE_MAGNITUDE = int.MaxValue + (long)1;
Throwing at least one of the operands long
The reason you get the error is indicated in the C # specs.
See C # 4.1.5 Specifications Section (Integral Types)
For binary numbers +, -, *, /,%, &, ^, |, == ,! =,>, <,> = and <= operators, operands are converted to type T, where T is the first int, uint, long and ulong , which can fully represent all possible values ββof both operands. Then the operation is performed using precision of type T, and the result type is T (or bool for relational operators). It is not allowed that one operand is of type long, and the other is of type ulong with binary operators.
In your case, since both operands of addition can be represented in int , therefore, the calculation is performed in an integer type. Explicit casting of one of the operands to long will result in a long result and, therefore, will not lead to an overflow error.
Habib source share