可能我会错过使用HashSet和HashCode的东西,但我不知道为什么这不能像我想的那样工作.我有一个覆盖HashCode的对象.我将对象添加到HashSet,然后我更改了一个属性(用于计算HashCode),然后我无法删除该对象.
public class Test
{
public string Code { get; set; }
public override int GetHashCode()
{
return (Code==null)?0: Code.GetHashCode();
}
}
public void TestFunction()
{
var test = new Test();
System.Collections.Generic.HashSet<Test> hashSet = new System.Collections.Generic.HashSet<Test>();
hashSet.Add(test);
test.Code = "Change";
hashSet.Remove(test); //this doesn´t remove from the hashset
}
Run Code Online (Sandbox Code Playgroud)