gpt4 book ai didi

c# - ImmutableHashSet .Contains 返回 false

转载 作者:太空狗 更新时间:2023-10-29 21:27:16 26 4
gpt4 key购买 nike

我有一个基本项列表(准确地说是来自 System.Collections.Immutable 的 ImmutableHashSet<ListItem>)并尝试调用以下代码

_baseList.Contains(derivedItem)

但这会返回false

即使下面的代码行都返回true

object.ReferenceEquals(_baseList.First(), derivedItem)
object.Equals(_baseList.First(), derivedItem)
_baseList.First().GetHashCode() == derivedItem.GetHashCode()

我什至可以编写以下内容并返回 true:

_baseList.OfType<DerivedClass>().Contains(derivedItem)

我做错了什么,我想避免写 .OfType 的东西。

编辑:

private ImmutableHashSet<BaseClass> _baseList;

public class BaseClass
{

}

public class DerivedClass : BaseClass
{

}

public void DoStuff()
{
var items = _baseList.OfType<DerivedClass>().ToList();
foreach (var derivedItem in items)
{
RemoveItem(derivedItem);
}
}

public void RemoveItem(BaseClass derivedItem)
{
if (_baseList.Contains(derivedItem))
{
//doesn't reach this place, since _baseList.Contains(derivedItem) returns false...
_baseList = _baseList.Remove(derivedItem);
}

//object.ReferenceEquals(_baseList.First(), derivedItem) == true
//object.Equals(_baseList.First(), derivedItem) == true
//_baseList.First().GetHashCode() == derivedItem.GetHashCode() == true
//_baseList.OfType<DerivedClass>().Contains(derivedItem) == true
}

编辑2:

这是我的问题的可重现代码,看起来像 ImmutableHashSet<>缓存 GetHashCode并且不比较当前的 GetHashCode使用列表中的条目,有没有办法告诉 ImmutableHashSet<>GetHashCode项目的数量可能不同,至少对于我当前正在检查的项目来说是不同的,因为嘿,它是该死的相同引用...

namespace ConsoleApplication1
{
class Program
{
private static ImmutableHashSet<BaseClass> _baseList;

static void Main(string[] args)
{
_baseList = ImmutableHashSet.Create<BaseClass>();
_baseList = _baseList.Add(new DerivedClass("B1"));
_baseList = _baseList.Add(new DerivedClass("B2"));
_baseList = _baseList.Add(new DerivedClass("B3"));
_baseList = _baseList.Add(new DerivedClass("B4"));
_baseList = _baseList.Add(new DerivedClass("B5"));

DoStuff();
Console.WriteLine(_baseList.Count); //output is 5 - put it should be 0...
Console.ReadLine();
}

private static void DoStuff()
{
var items = _baseList.OfType<DerivedClass>().ToList();
foreach (var derivedItem in items)
{
derivedItem.BaseString += "Change...";
RemoveItem(derivedItem);
}
}

private static void RemoveItem(BaseClass derivedItem)
{
if (_baseList.Contains(derivedItem))
{
_baseList = _baseList.Remove(derivedItem);
}
}
}

public abstract class BaseClass
{
private string _baseString;
public string BaseString
{
get { return _baseString; }
set { _baseString = value; }
}

public BaseClass(string baseString)
{
_baseString = baseString;
}

public override int GetHashCode()
{
unchecked
{
int hashCode = (_baseString != null ? _baseString.GetHashCode() : 0);
return hashCode;
}
}
}
public class DerivedClass : BaseClass
{
public DerivedClass(string baseString)
: base(baseString)
{

}
}
}

如果我要更改 ImmutableHashSet<>ImmutableList<>代码工作正常,所以如果你们没有想出任何好主意,我会切换到列表。

最佳答案

在字典和其他与哈希相关的数据结构中使用的对象应该具有不可变的身份——所有与哈希相关的数据结构都假定一旦您将对象添加到字典中,其哈希码就不会改变。

此代码无法正常工作:

    private static void DoStuff()
{
var items = _baseList.OfType<DerivedClass>().ToList();
foreach (var derivedItem in items)
{
derivedItem.BaseString += "Change...";
RemoveItem(derivedItem);
}
}

private static void RemoveItem(BaseClass derivedItem)
{
if (_baseList.Contains(derivedItem))
{
_baseList = _baseList.Remove(derivedItem);
}
}
RemoveItem() 中的

_baseList.Contains(),由 DoStuff() 调用,将为每个项目返回 false,因为您更改了存储项的标识 - 它的 BaseString 属性。

关于c# - ImmutableHashSet .Contains 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com