今天我用 ConcurrentDictionary 和 Dictionary 做了一些测试:
class MyTest
{
public int Row { get; private set; }
public int Col { get; private set; }
public string Value { get; private set; }
public MyTest(int row, int col, string value)
{
this.Col = col;
this.Row = row;
this.Value = value;
}
public override bool Equals(object obj)
{
MyTest other = obj as MyTest;
return base.Equals(other);
}
public override int GetHashCode()
{
return (Col.GetHashCode() ^ Row.GetHashCode() ^ Value.GetHashCode());
}
}
使用上面的实体,我创建并填充了 ConcurrentDictionary 和 Dictionary 并尝试了以下代码:
ConcurrentDictionary<MyTest, List<MyTest>> _test = new ConcurrentDictionary<MyTest, List<MyTest>>();
Dictionary<MyTest, List<MyTest>> _test2 = new Dictionary<MyTest, List<MyTest>>();
MyTest dunno = _test.Values.AsParallel().Select(x => x.Find(a => a.Col == 1 && a.Row == 1)).FirstOrDefault();
MyTest dunno2 = _test2.Values.AsParallel().Select(x => x.Find(a => a.Col == 1 && a.Row == 1)).FirstOrDefault();
第一个返回值,但第二个没有,我做错了什么?
这是用于添加值的代码:
_test.AddOrUpdate(cell10,
new List<MyTest>
{
new MyTest(1, 1, "ovpSOMEVALUEValue"),
new MyTest(1, 2, "ocpSOMEVALUEValue")
},
(key, value) => value = new List<MyTest>());
_test2.Add(cell10,
new List<MyTest>
{
new MyTest(1, 1, "ovpSOMEVALUEValue"),
new MyTest(1, 2, "ocpSOMEVALUEValue")
}
);