LINQ 和溢出异常?

LINQ Sum OverflowException?(LINQ 和溢出异常?)
本文介绍了LINQ 和溢出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 EventLogEntry 实现了一个自定义的 IEqualityComparer.

I've implemented a custom IEqualityComparer for EventLogEntry.

public class EventLogEntryListComparison :
    IEqualityComparer<List<EventLogEntry>>,
    IEqualityComparer<EventLogEntry>

对于IEqualityComparer>,GetHashCode函数很简单.

For IEqualityComparer<List<EventLogEntry>>, the GetHashCode function is very simple.

public int GetHashCode(List<EventLogEntry> obj)
{
    return obj.Sum(entry => 23 * GetHashCode(entry));
}

但是,对于某些条目,这会引发 OverflowException.

However, this throws an OverflowException for certain entries.

"Arithmetic operation resulted in an overflow."
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)
   at <snip>.Diagnostics.EventLogAnalysis.EventLogEntryListComparison.GetHashCode(List`1 obj) in C:dev<snip>Diagnostics.EventLogAnalysisEventLogEntryListComparison.cs:line 112
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at <snip>.Diagnostics.EventLogAnalysis.Program.AnalyseMachine(String validMachineName) in C:dev<snip>.Diagnostics.EventLogAnalysisProgram.cs:line 104
   at System.Threading.Tasks.Parallel.<>c__DisplayClass2d`2.<ForEachWorker>b__23(Int32 i)
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()

在调试时尝试得到相同的错误并且无法在即时窗口中出现后,我将代码更改为这个并再见 OverflowException?

After trying to get the same error whilst debugging and couldn't in the immediate window, I changed the code to this and bye bye OverflowException?

int total = 0;
foreach (var eventLogEntry in obj)
{
    total += GetHashCode(eventLogEntry);
}

return total;

LINQ 的 Sum 函数有何不同?

How is it that LINQ's Sum function behaving differently?

编辑 2

感谢一些评论,更正和预期的 GetHashCode 函数现在如下,

Thanks to a few comments, the corrected and intended GetHashCode function is now as follows,

public int GetHashCode(List<EventLogEntry> obj)
{
    return unchecked(obj.Aggregate(17,
        (accumulate, entry) =>
        accumulate * 23 + GetHashCode(entry)));
}

推荐答案

LINQ 的 Enumerable.Sum(...) 方法在 checked 块内执行加法.这意味着如果总和溢出,他们会故意抛出异常.

LINQ's Enumerable.Sum(...) methods perform the additions inside a checked block. This means that they deliberately throw an exception if the sum overflows.

您的 sum 不在 checked 块内,因此它是否引发异常取决于...是从 checked 块内调用它,还是我相信在程序集上的一个属性.

Your sum is not inside a checked block, so whether or not it throws an exception depends on... whether it is called from inside a checked block, or a property on the assembly I believe.

这篇关于LINQ 和溢出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)