Improve overflow detection algorithm in XML documentation examples #47975
+16
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The XML documentation examples in
include-tag.cs
contained a flawed overflow detection algorithm that only caught the specific case where one parameter equalsint.MaxValue
and the other is positive. This approach missed many overflow scenarios like(int.MaxValue - 5) + 10
.Changes Made
Integer Add Method
Replaced the manual overflow check with .NET's built-in
checked
context:Double Add Method
Enhanced floating-point overflow detection with proper infinity checks:
Benefits
checked
context<include>
tags and exception documentationThe changes align with maintainer feedback suggesting the use of
checked(a + b)
while keeping the example focused on XML documentation features rather than complex mathematical algorithms.Fixes #41596.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.