C#字典包含键 [英] C# Dictionary ContainsKey

查看:149
本文介绍了C#字典包含键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是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()等于(的ToString());
}

public override int GetHashCode()
{
返回ToString()。GetHashCode();
}
}

然后我有一个字典,如下所示: / p>

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

....

{
//总是评估为假!
if(_filteredStatisticsDict.ContainsKey(statisticsFilter)== false)
{
_filteredStatisticsDict.Add(statisticsFilter,new Statistics());
}
}


解决方案

使用您给我们的代码进行复制。

  using System; 
使用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()
{
返回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)); //打印真
}
}


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());
    }
}

解决方案

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
    }
}

这篇关于C#字典包含键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆