The Labs \ Source Viewer \ SSCLI \ System.Collections \ CaseInsensitiveHashCodeProvider

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class: CaseInsensitiveHashCodeProvider
  18. **
  19. **
  20. ** Purpose: Designed to support hashtables which require
  21. ** case-insensitive behavior while still maintaining case,
  22. ** this provides an efficient mechanism for getting the
  23. ** hashcode of the string ignoring case.
  24. **
  25. **
  26. ============================================================*/
  27. namespace System.Collections
  28. {
  29.     //This class does not contain members and does not need to be serializable
  30.     using System;
  31.     using System.Collections;
  32.     using System.Globalization;
  33.    
  34.     [Serializable()]
  35.     [Obsolete("Please use StringComparer instead.")]
  36.     [System.Runtime.InteropServices.ComVisible(true)]
  37.     public class CaseInsensitiveHashCodeProvider : IHashCodeProvider
  38.     {
  39.         private TextInfo m_text;
  40.         private static CaseInsensitiveHashCodeProvider m_InvariantCaseInsensitiveHashCodeProvider = null;
  41.        
  42.         public CaseInsensitiveHashCodeProvider()
  43.         {
  44.             m_text = CultureInfo.CurrentCulture.TextInfo;
  45.         }
  46.        
  47.         public CaseInsensitiveHashCodeProvider(CultureInfo culture)
  48.         {
  49.             if (culture == null) {
  50.                 throw new ArgumentNullException("culture");
  51.             }
  52.             m_text = culture.TextInfo;
  53.         }
  54.        
  55.         public static CaseInsensitiveHashCodeProvider Default {
  56.             get { return new CaseInsensitiveHashCodeProvider(CultureInfo.CurrentCulture); }
  57.         }
  58.        
  59.         public static CaseInsensitiveHashCodeProvider DefaultInvariant {
  60.             get {
  61.                 if (m_InvariantCaseInsensitiveHashCodeProvider == null) {
  62.                     m_InvariantCaseInsensitiveHashCodeProvider = new CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture);
  63.                 }
  64.                 return m_InvariantCaseInsensitiveHashCodeProvider;
  65.             }
  66.         }
  67.        
  68.         public int GetHashCode(object obj)
  69.         {
  70.             if (obj == null) {
  71.                 throw new ArgumentNullException("obj");
  72.             }
  73.            
  74.             string s = obj as string;
  75.             if (s == null) {
  76.                 return obj.GetHashCode();
  77.             }
  78.            
  79.             return m_text.GetCaseInsensitiveHashCode(s);
  80.         }
  81.     }
  82. }

Developer Fusion