See the code below, I just want to understand the reason behind that…
const int a = 2147483647;
const int b = 2147483647;
int c = a + b; // it doesn't allow to compile!!!
int a = 2147483647;
int b = 2147483647;
int c = a + b; // it allows to compile!!!
constexpressions are resolved at compile-time, non-constexpressions are resolved at runtime. Each have different types of overflow checking contexts by default. According to the C# specification:Which is why you’re not seeing a runtime error when you use local variables to do the arithmetic. As for the
constcalculation:Which is why you’re seeing a compile-time error with your
constcalculation.More information about checked and unchecked on MSDN.