Make it easier to use StringComparison & StringComparer with GetHashCode (#8633)
authorAlexander Radchenko <radchenkosasha@gmail.com>
Thu, 15 Dec 2016 23:24:14 +0000 (06:24 +0700)
committerDan Moseley <danmose@microsoft.com>
Thu, 15 Dec 2016 23:24:14 +0000 (15:24 -0800)
* Make it easier to use StringComparison & StringComparer with GetHashCode

* model.xml

src/mscorlib/model.xml
src/mscorlib/src/System/String.Comparison.cs
src/mscorlib/src/System/StringComparer.cs

index afc6af1f4c7de81b5577c3dc60513d0efd9223ed..31b00eb867f80f503bccb8bcda910c34d5cb46b2 100644 (file)
       <Member Name="get_Length" />
       <Member Name="GetEnumerator" />
       <Member Name="GetHashCode" />
+      <Member Name="GetHashCode(System.StringComparison)" />
       <Member Name="GetTypeCode" />
       <Member Name="IndexOf(System.Char)" />
       <Member Name="IndexOf(System.Char,System.Int32)" />
       <Member Name="Create(System.Globalization.CultureInfo,System.Boolean)" />
       <Member Name="Equals(System.Object,System.Object)" />
       <Member Name="Equals(System.String,System.String)" />
+      <Member Name="FromComparison(System.StringComparison)" />
       <Member Name="get_CurrentCulture" />
       <Member Name="get_CurrentCultureIgnoreCase" />
       <Member Name="get_InvariantCulture" />
index da991706f8a12fcc90109b9b2c2285e33235a3e2..07f2f9de224b93339c212fd701df77e09a9506f9 100644 (file)
@@ -997,6 +997,11 @@ namespace System
             return GetLegacyNonRandomizedHashCode();
         }
 
+        // Gets a hash code for this string and this comparison. If strings A and B and comparition C are such
+        // that String.Equals(A, B, C), then they will return the same hash code with this comparison C.
+        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+        public int GetHashCode(StringComparison comparisonType) => StringComparer.FromComparison(comparisonType).GetHashCode(this);
+
         // Use this if and only if you need the hashcode to not change across app domains (e.g. you have an app domain agile
         // hash table).
         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
index af0b595855e08e4b270bcc7908acf590806c9a44..02e8d71d0ee6e2662aea7cd2059248dc7cb7b09d 100644 (file)
@@ -62,6 +62,28 @@ namespace System {
             }
         }
 
+        // Convert a StringComparison to a StringComparer
+        public static StringComparer FromComparison(StringComparison comparisonType)
+        {
+            switch (comparisonType)
+            {
+                case StringComparison.CurrentCulture:
+                    return CurrentCulture;
+                case StringComparison.CurrentCultureIgnoreCase:
+                    return CurrentCultureIgnoreCase;
+                case StringComparison.InvariantCulture:
+                    return InvariantCulture;
+                case StringComparison.InvariantCultureIgnoreCase:
+                    return InvariantCultureIgnoreCase;
+                case StringComparison.Ordinal:
+                    return Ordinal;
+                case StringComparison.OrdinalIgnoreCase:
+                    return OrdinalIgnoreCase;
+                default:
+                    throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), nameof(comparisonType));
+            }
+        }
+
         public static StringComparer Create(CultureInfo culture, bool ignoreCase) {
             if( culture == null) {
                 throw new ArgumentNullException(nameof(culture));