1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Runtime.Serialization;
6 
7 namespace System.Collections.Generic
8 {
9     // NonRandomizedStringEqualityComparer is the comparer used by default with the Dictionary<string,...>
10     // We use NonRandomizedStringEqualityComparer as default comparer as it doesnt use the randomized string hashing which
11     // keeps the performance not affected till we hit collision threshold and then we switch to the comparer which is using
12     // randomized string hashing.
13     [Serializable] // Required for compatibility with .NET Core 2.0 as we exposed the NonRandomizedStringEqualityComparer inside the serialization blob
14 #if CORECLR
15     internal
16 #else
17     public
18 #endif
19     sealed class NonRandomizedStringEqualityComparer : EqualityComparer<string>, ISerializable
20     {
21         internal static new IEqualityComparer<string> Default { get; } = new NonRandomizedStringEqualityComparer();
22 
NonRandomizedStringEqualityComparer()23         private NonRandomizedStringEqualityComparer() { }
24 
25         // This is used by the serialization engine.
NonRandomizedStringEqualityComparer(SerializationInfo information, StreamingContext context)26         private NonRandomizedStringEqualityComparer(SerializationInfo information, StreamingContext context) { }
27 
Equals(string x, string y)28         public sealed override bool Equals(string x, string y) => string.Equals(x, y);
29 
GetHashCode(string obj)30         public sealed override int GetHashCode(string obj) => obj?.GetLegacyNonRandomizedHashCode() ?? 0;
31 
GetObjectData(SerializationInfo info, StreamingContext context)32         public void GetObjectData(SerializationInfo info, StreamingContext context)
33         {
34             // We are doing this to stay compatible with .NET Framework.
35             info.SetType(typeof(GenericEqualityComparer<string>));
36         }
37     }
38 }
39