C#:GetHashCode、Equals 和 Dictionary 导致 IndexOutOfRangeException?

发布于 2024-12-09 01:22:23 字数 1505 浏览 1 评论 0原文

可能的重复:
字典何时在 Add 或 时抛出 IndexOutOfRangeException包含Key?

这个很时髦。我无法轻松地探究到底。在日志中发现异常,挖出了一些旧代码。不要问我为什么这样写,因为我不知道。问题实际上是在设置字典的 Item 时抛出 IndexOutOfRangeException 的条件是什么。事情是这样的:

public MyEnum { Undefined = 0, B = 1, C = 2, D = 16, All = B | C | D }

public class MC 
{
  private int _hashCode;
  private int _i;
  private MyEnum _e;

  public MC(int i, MyEnum e)
  {
     _i = i;
     _e = e;
     SetHashCode();
  }

  private SetHashCode()
  {
    _hashCode = _i * 31 + (int)e;
  }

  public override bool Equals(object other)
  {
    if (!(obj is MC))
    {
      return false;
    }
    MC other = (MC)obj;
    return ((_i == other._i) && (_e == other._e));
  }

  public override int GetHashCode()
  {
    return _hashCode;
  }
}

...

var d = new Dictionary<MC, DateTime>();
...
// Create and populate the list of MCs
var mcs = new List<MC>();
...
foreach (MC mc in mcs) 
{
  ...
  d[mc] = DateTime.UtcNow; // Pukes here
}

例外是:

System.IndexOutOfRangeException, Source: mscorlib    Index was outside the bounds of the array.       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)

想法如何使生产线失败?不要将你的注意力转移到错误的方向,但我认为 Equals 和 GetHashCode 有一些可疑的地方,但到目前为止我无法证明这一点 - 没有使用单元测试框架进行重现。

Possible Duplicate:
When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?

This one is funky. I can't get to the bottom of it with ease. Found an exception in the log and dug out some old code. Don't ask me about why this is written this way because I have no idea. The question really is what could be conditions for IndexOutOfRangeException to be thrown when the Item of the dictionary is being set. Here is how the thing looks:

public MyEnum { Undefined = 0, B = 1, C = 2, D = 16, All = B | C | D }

public class MC 
{
  private int _hashCode;
  private int _i;
  private MyEnum _e;

  public MC(int i, MyEnum e)
  {
     _i = i;
     _e = e;
     SetHashCode();
  }

  private SetHashCode()
  {
    _hashCode = _i * 31 + (int)e;
  }

  public override bool Equals(object other)
  {
    if (!(obj is MC))
    {
      return false;
    }
    MC other = (MC)obj;
    return ((_i == other._i) && (_e == other._e));
  }

  public override int GetHashCode()
  {
    return _hashCode;
  }
}

...

var d = new Dictionary<MC, DateTime>();
...
// Create and populate the list of MCs
var mcs = new List<MC>();
...
foreach (MC mc in mcs) 
{
  ...
  d[mc] = DateTime.UtcNow; // Pukes here
}

And the exception is:

System.IndexOutOfRangeException, Source: mscorlib    Index was outside the bounds of the array.       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)

Ideas how to make the line to fail? Don't what to diverge your attention to the wrong direction but I thought there was something fishy with the Equals and GetHashCode but I couldn't prove it so far - no repro using a unit testing framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2024-12-16 01:22:23

您收到的错误通常是由多线程、未锁定的并发访问字典引起的。

请参阅: 字典何时抛出 IndexOutOfRangeException添加或包含Key?

The error you are getting is often caused by multi-threaded, un-locked concurrent access to a dictionary.

See: When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文