12

I tried the following code :

int x, y;
x = y = int.MaxValue;
int result = x + y;

This code work fine and result will contain -2 (I know why).

But when doing this :

const int x = int.MaxValue;
const int y = int.MaxValue;
int result = x + y;

This will not compile because of overflow problem.Why ?

asked Nov 1, 2013 at 20:40
1
  • 1
    I am totally intrigued by this question. Commented Nov 1, 2013 at 20:42

1 Answer 1

13

Because both x and y are compile-time constants, so is x + y. The compiler knows that the result will overflow, so it complains about it.

You can fix this by using an unchecked expression:

int result = unchecked(x + y);

From section 7.6.12 of the C# 5 specification - after listing +, -, / and *:

When one of the above operations produce a result that is too large to represent in the destination type, the context in which the operation is performed controls the resulting behavior:

  • In a checked context, if the operation is a constant expression (§7.19), a compile-time error occurs.
  • In an unchecked context, the result is truncated by discarding any high-order bits that do not fit in the destination type.

For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any checked or unchecked operators or statements, the default overflow checking context is unchecked unless external factors (such as compiler switches and execution environment configuration) call for checked evaluation.

For constant expressions (expressions that can be fully evaluated at compile-time), the default overflow checking context is always checked. Unless a constant expression is explicitly placed in an unchecked context, overflows that occur during the compile-time evaluation of the expression always cause compile-time errors.

answered Nov 1, 2013 at 20:42
Sign up to request clarification or add additional context in comments.

7 Comments

Maybe off-topic but, does unchecked reduce compile time when used for large bodies?
@huseyintugrulbuyukisik: I would be very surprised if you could find a real-world example where the difference was measurable.
Question Jon: Is it reasonable that one would actually want to do this?
@John: There are times when it's useful to be able to express constants in an unchecked way, yes - e.g. for bit patterns. Fairly rare though.
@John One example is when you need to specify a HRESULT as an ordinary int which is signed, from the hexadecimal representation. It looks like int hr = unchecked((int)0x80070005);.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.