NonRandomizedStringEqualityComparer.cs 1.3 KB

12345678910111213141516171819202122232425
  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. using System.Runtime.Serialization;
  5. namespace System.Collections.Generic
  6. {
  7. // NonRandomizedStringEqualityComparer is the comparer used by default with the Dictionary<string,...>
  8. // We use NonRandomizedStringEqualityComparer as default comparer as it doesnt use the randomized string hashing which
  9. // keeps the performance not affected till we hit collision threshold and then we switch to the comparer which is using
  10. // randomized string hashing.
  11. [Serializable] // Required for compatibility with .NET Core 2.0 as we exposed the NonRandomizedStringEqualityComparer inside the serialization blob
  12. // Needs to be public to support binary serialization compatibility
  13. public sealed class NonRandomizedStringEqualityComparer : EqualityComparer<string>
  14. {
  15. internal static new IEqualityComparer<string> Default { get; } = new NonRandomizedStringEqualityComparer();
  16. private NonRandomizedStringEqualityComparer() { }
  17. public sealed override bool Equals(string x, string y) => string.Equals(x, y);
  18. public sealed override int GetHashCode(string obj) => obj?.GetNonRandomizedHashCode() ?? 0;
  19. }
  20. }