Search code examples
c#hashsetreferenceequals

How to build a Set in C# that compares items only by reference equality, instead of HashCode used by HashSet?


For example, what if I want to have a set of strings with different references, allowing them to have the same values?

String s1 = "String1";
String s2 = "String1";
Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2));
Console.WriteLine("{0} interned: {1}", s1,
                  String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes");

String suffix = "A";
String s3 = "String" + suffix;
String s4 = "String" + suffix;
Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4));
Console.WriteLine("{0} interned: {1}", s3,
                  String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");

// The example displays the following output:
//       s1 = s2: True
//       String1 interned: Yes
//       s3 = s4: False
//       StringA interned: No

I need to have s3 and s4 to be recognized as different objects and exist in the same Set simultaneously, even if they have the same value. In contrast, s1 and s2 should be only present once, because they have the same reference.

But it is not only a problem of strings. How to have a Set for any generic type that only compares reference equality?


Solution

  • You can use ReferenceEqualityComparer for this purpose but be warned if you use it for string, you may have issue with reference due to how they are handled internally like the comments said.

    var s1 = new string("Hello");
    var s2 = new string("Hello");
    var s3 = new string("Hello");
    
    Console.WriteLine(object.ReferenceEquals(s1, s2)); // false
    var hashset = new HashSet<string>(ReferenceEqualityComparer.Instance)
    {
        s1,
        s2
    };
    
    Console.WriteLine(hashset.Count); // 2
    Console.WriteLine(hashset.Contains(s1)); // true
    Console.WriteLine(hashset.Contains(s3)); // false