Enumerable.Sum() always executes addition in a
checked context, so an OverflowException will be
thrown if the value exceeds MaxValue, even if an unchecked context was specified. Therefore, using this method inside an
unchecked context will only make the code more confusing, since the behavior will still be checked.
This rule raises an issue when an unchecked context is specified for a Sum on integer types.
Exceptions
When the Sum call is inside a try-catch block,
no issues are reported, since the exception is properly handled.
void Add(List<int> list)
{
unchecked
{
try
{
int total = list.Sum();
}
catch (System.OverflowException e)
{
// Exception handling
}
}
}