C# 字典包含Key

发布于 2024-11-09 10:35:22 字数 972 浏览 0 评论 0原文

我的问题是 ContainsKey 总是返回 false,即使它们已添加密钥并且 .Equals 计算结果为 true。

我有以下课程:

public class StatisticsFilter 
{
    private String someString1;
    private String someString2;
    .....

    public override string ToString()
    {
        return string.Format("{0}-{1}-{2}-{3}-{4}", someString1, someString2, ...)
    }

    public override bool Equals(object obj)
    {            
        return obj.ToString().Equals(ToString());
    }

    public override int GetHashCode()
    {
        return ToString().GetHashCode();
    }
}

然后我有一本如下所示的字典:

private readonly IDictionary<StatisticsFilter, Statistics> _filteredStatisticsDict =
            new Dictionary<StatisticsFilter, Statistics>();

....

{
    // ALWAYS EVALUATES TO FALSE!
    if (_filteredStatisticsDict.ContainsKey(statisticsFilter) == false)
    {
         _filteredStatisticsDict.Add(statisticsFilter, new Statistics());
    }
}

My problem is ContainsKey is always returning false even when they key has been added and .Equals evaluates to true.

I have the following class:

public class StatisticsFilter 
{
    private String someString1;
    private String someString2;
    .....

    public override string ToString()
    {
        return string.Format("{0}-{1}-{2}-{3}-{4}", someString1, someString2, ...)
    }

    public override bool Equals(object obj)
    {            
        return obj.ToString().Equals(ToString());
    }

    public override int GetHashCode()
    {
        return ToString().GetHashCode();
    }
}

I then have a dictionary that looks like this:

private readonly IDictionary<StatisticsFilter, Statistics> _filteredStatisticsDict =
            new Dictionary<StatisticsFilter, Statistics>();

....

{
    // ALWAYS EVALUATES TO FALSE!
    if (_filteredStatisticsDict.ContainsKey(statisticsFilter) == false)
    {
         _filteredStatisticsDict.Add(statisticsFilter, new Statistics());
    }
}

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

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

发布评论

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

评论(2

两相知 2024-11-16 10:35:22

无法使用您提供给我们的代码进行重现。

using System;
using System.Collections.Generic;

public class StatisticsFilter 
{
    private String someString1;
    private String someString2;

    public StatisticsFilter(string x, string y)
    {
        this.someString1 = x;
        this.someString2 = y;
    }

    public override string ToString()
    {
        return string.Format("{0}-{1}xyz", someString1, someString2);
    }

    public override bool Equals(object obj)
    {            
        return obj.ToString().Equals(ToString());
    }

    public override int GetHashCode()
    {
        return ToString().GetHashCode();
    }
}

class Test
{
    static void Main()
    {
        var dict = new Dictionary<StatisticsFilter, int>();

        var sf1 = new StatisticsFilter("hello", "there");
        var sf2 = new StatisticsFilter("hello", "there");

        dict[sf1] = 10;
        Console.WriteLine(dict.ContainsKey(sf2)); // Prints true
    }
}

Unable to reproduce with the code you've given us.

using System;
using System.Collections.Generic;

public class StatisticsFilter 
{
    private String someString1;
    private String someString2;

    public StatisticsFilter(string x, string y)
    {
        this.someString1 = x;
        this.someString2 = y;
    }

    public override string ToString()
    {
        return string.Format("{0}-{1}xyz", someString1, someString2);
    }

    public override bool Equals(object obj)
    {            
        return obj.ToString().Equals(ToString());
    }

    public override int GetHashCode()
    {
        return ToString().GetHashCode();
    }
}

class Test
{
    static void Main()
    {
        var dict = new Dictionary<StatisticsFilter, int>();

        var sf1 = new StatisticsFilter("hello", "there");
        var sf2 = new StatisticsFilter("hello", "there");

        dict[sf1] = 10;
        Console.WriteLine(dict.ContainsKey(sf2)); // Prints true
    }
}
枉心 2024-11-16 10:35:22

是否期望“ToString()”成为关键?我认为通过将 Dictionary 声明更改为: Dictionary

// not always be false
if (_filteredStatisticsDict.ContainsKey(statistics.ToString() == false)
{
    _filteredStatisticsDict.Add(statisticsFilter.ToString(), newStatisitcs());
}

如果我理解您要实现的目标,那么您将获得所需的结果,这应该可行。
使用此方法,字典键基于过滤器的内容。

Are expecting the 'ToString()' to be the key? I think you will get the desired result by changing the Dictionary declaration to: Dictionary<string, Statistics>

// not always be false
if (_filteredStatisticsDict.ContainsKey(statistics.ToString() == false)
{
    _filteredStatisticsDict.Add(statisticsFilter.ToString(), newStatisitcs());
}

If I understand what you are trying to accomplish, this should work.
With this method the dictionary key is based on the content of the filter.

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