小编Nac*_*cho的帖子

HashSet不会删除对象

可能我会错过使用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)

c# hashset

3
推荐指数
1
解决办法
1192
查看次数

标签 统计

c# ×1

hashset ×1