C# Check if dictionary contains key which is a reference type [duplicate]

我是研究僧i 提交于 2021-01-29 06:11:25

问题


If I have a Dictionary<int[], int>, which contains 2 entries

{1, 2, 3} , 1 and
{4, 5, 6} , 2

How do I check whether the dictionary contains the key {1, 2, 3}? If i do:

if (dictionary.ContainsKey(new int[] {1,2,3})
{
  // do things
}

It will not return the correct result as the created array is different from the key array. I know you can override the Equals method for a custom class, but is there another way to check if a dictionary which has an array as the key, contains an entry whose key array has the same values within it as the comparison array?


回答1:


Well, arrays are compared by reference so we have

int[] a = new int[] {1, 2, 3};

int[] b = new int[] {1, 2, 3}; // same content, different reference

// Prints "No"
Console.WriteLine(a.Equals(b) ? "Yes" : "No");

Dictionary<int[], string> dict = new Dictionary<int[], string>() {{
  a, "Some Value"}};

// Prints "Not Found"
Console.WriteLine(dict.TryGetValue(b, out var value) ? value : "Not Found");

So we have to explain .Net how to compare arrays; we can do it with a comparer:

public class SequenceEqualityComparer<T> : IEqualityComparer<IEnumerable<T>> {
  public bool Equals(IEnumerable<T> x, IEnumerable<T> y) {
    if (ReferenceEquals(x, y))
      return true;
    else if (null == x || null == y)
      return false;

    return Enumerable.SequenceEqual(x, y, EqualityComparer<T>.Default); 
  }

  public int GetHashCode(IEnumerable<T> obj) =>
    obj == null ? 0 : obj.FirstOrDefault()?.GetHashCode() ?? 0;
}

And we should declare the dictionary while mentioning the comparer:

Dictionary<int[], string> dict = 
  new Dictionary<int[], string>(new SequenceEqualityComparer<int>()) {
    {a, "Some Value"}};

Now we can do business as usual:

// Returns "Some Value" 
Console.WriteLine(dict[b]);



回答2:


As quick workaround, you can use SequenceEqual and Any methods. It'll work, if you need a value comparison only

if (dictionary.Keys.Any(key => key.SequenceEqual(new [] {1, 2, 3})))
{
}



回答3:


You can try,

if (dictionary.Any(x => Enumerable.SequenceEqual(new int[] { 1, 2, 3 }, x.Key)))
    Console.WriteLine("There is exists one.");

Note : Method using System.Linq Library.



来源:https://stackoverflow.com/questions/63129018/c-sharp-check-if-dictionary-contains-key-which-is-a-reference-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!