Practice mode C#

What happens when C# evaluates checked(x + 1) where x is an int variable holding int.MaxValue?

Correct answer B. An OverflowException is thrown

Explanation

Inside a checked expression or block, integral arithmetic that overflows its type throws System.OverflowException instead of wrapping. Since int.MaxValue + 1 does not fit in an int, the runtime raises the exception. C# never silently widens to long. Use checked for financial or counting code where a wrapped value would be a silent bug, and unchecked where wrapping is intended, such as hashing.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.