3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: CaseInsensitiveHashCodeProvider
10 ** <OWNER>[....]</OWNER>
13 ** Purpose: Designed to support hashtables which require
14 ** case-insensitive behavior while still maintaining case,
15 ** this provides an efficient mechanism for getting the
16 ** hashcode of the string ignoring case.
19 ============================================================*/
20 namespace System.Collections {
21 //This class does not contain members and does not need to be serializable
23 using System.Collections;
24 using System.Globalization;
25 using System.Diagnostics.Contracts;
28 [Obsolete("Please use StringComparer instead.")]
29 [System.Runtime.InteropServices.ComVisible(true)]
30 public class CaseInsensitiveHashCodeProvider : IHashCodeProvider {
31 private TextInfo m_text;
32 private static volatile CaseInsensitiveHashCodeProvider m_InvariantCaseInsensitiveHashCodeProvider = null;
34 public CaseInsensitiveHashCodeProvider() {
35 m_text = CultureInfo.CurrentCulture.TextInfo;
38 public CaseInsensitiveHashCodeProvider(CultureInfo culture) {
40 throw new ArgumentNullException("culture");
42 Contract.EndContractBlock();
43 m_text = culture.TextInfo;
46 public static CaseInsensitiveHashCodeProvider Default
50 Contract.Ensures(Contract.Result<CaseInsensitiveHashCodeProvider>() != null);
52 return new CaseInsensitiveHashCodeProvider(CultureInfo.CurrentCulture);
56 public static CaseInsensitiveHashCodeProvider DefaultInvariant
60 Contract.Ensures(Contract.Result<CaseInsensitiveHashCodeProvider>() != null);
62 if (m_InvariantCaseInsensitiveHashCodeProvider == null) {
63 m_InvariantCaseInsensitiveHashCodeProvider = new CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture);
65 return m_InvariantCaseInsensitiveHashCodeProvider;
69 public int GetHashCode(Object obj) {
71 throw new ArgumentNullException("obj");
73 Contract.EndContractBlock();
75 String s = obj as String;
77 return obj.GetHashCode();
80 return m_text.GetCaseInsensitiveHashCode(s);