Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 20, 2026

The documentation incorrectly stated that arithmetic overflow in unchecked context always truncates. Integer division of int.MinValue / -1 (or long.MinValue / -1) throws OverflowException even in unchecked context, per C# spec §12.12.3 implementation-defined behavior.

Changes

  • arithmetic-operators.md: Added NOTE in "Integer arithmetic overflow" section documenting the special case where division can throw despite unchecked context
  • ArithmeticOperators.cs: Added IntegerDivisionOverflow snippet demonstrating the exception:
int a = int.MinValue;
int b = -1;
try
{
    int c = unchecked(a / b);  // Still throws OverflowException
}
catch (OverflowException)
{
    Console.WriteLine($"Overflow occurred when dividing {a} by {b}.");
}

The .NET runtime throws because the mathematical result (2,147,483,648) exceeds int.MaxValue and cannot be represented.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • learn.microsoft.com
    • Triggering command: /home/REDACTED/work/docs/docs/docs/csharp/language-reference/operators/snippets/shared/bin/Debug/net10.0/operators /home/REDACTED/work/docs/docs/docs/csharp/language-reference/operators/snippets/shared/bin/Debug/net10.0/operators (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Integral division can trigger OverflowException even in unchecked context</issue_title>
<issue_description>### Type of issue

Missing information

Description

The documentation states (emphasis mine):

If integer arithmetic overflow occurs, the overflow-checking context, which can be checked or unchecked, controls the resulting behavior:

  • In a checked context, if overflow happens in a constant expression, a compile-time error occurs. Otherwise, when the operation is performed at run time, an OverflowException is thrown.
  • In an unchecked context, the result is truncated by discarding any high-order bits that don't fit in the destination type.

This is not correct. Even in unchecked context, it is possible for OverflowException to be thrown. The specification §12.12.3 clearly states the following:

If the left operand is the smallest representable int or long value and the right operand is –1, an overflow occurs. In a checked context, this causes a System.ArithmeticException (or a subclass thereof) to be thrown. In an unchecked context, it is implementation-defined as to whether a System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes unreported with the resulting value being that of the left operand.

And the following code:

int a = int.MinValue;
int b = -1;
Console.WriteLine(a / b);

does indeed produce System.OverflowException in .NET9 and .NET10.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#arithmetic-overflow-and-division-by-zero

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/operators/arithmetic-operators.md

Document Version Independent Id

2536ab40-6d7c-1816-19d2-d2dc33991d73

Platform Id

831f8f7a-d38c-7d02-68b2-57f8fdcf6c11

Article author

@pkulikov

Metadata

  • ID: a9963fd1-083f-1e8c-0656-b04281c64849
  • PlatformId: 831f8f7a-d38c-7d02-68b2-57f8fdcf6c11
  • Service: dotnet-csharp
  • Sub-service: lang-reference

Related Issues</issue_description>

Comments on the Issue (you are @copilot in this section)

@BillWagner Adding @tannergooding to check my work.

As the OP quotes, the standard says the behavior is implementation defined. I believe the roslyn implementation does truncate, as this article states.

@tannergooding > I believe the roslyn implementation does truncate, as this article states.

@BillWagner Roslyn just defers down to the underlying runtime implementation. The underlying runtime then requires that div throw ArithmeticException (of which OverflowException is a derived type) if the result cannot be represented in the target type (i.e. MinValue / -1).

This then mirrors what x86/x64 hardware does, which is "fault" (effectively throw an exception in hardware) on such an operation.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…d context

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix integer division overflow handling in unchecked context Document integer division overflow exception in unchecked context Jan 20, 2026
Copilot AI requested a review from BillWagner January 20, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integral division can trigger OverflowException even in unchecked context

2 participants