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

  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: CaseInsensitiveComparer
  18. **
  19. **
  20. **
  21. ============================================================*/
  22. namespace System.Collections
  23. {
  24.     //This class does not contain members and does not need to be serializable
  25.     using System;
  26.     using System.Collections;
  27.     using System.Globalization;
  28.    
  29.     [Serializable()]
  30.     [System.Runtime.InteropServices.ComVisible(true)]
  31.     public class CaseInsensitiveComparer : IComparer
  32.     {
  33.         private CompareInfo m_compareInfo;
  34.         private static CaseInsensitiveComparer m_InvariantCaseInsensitiveComparer;
  35.        
  36.         public CaseInsensitiveComparer()
  37.         {
  38.             m_compareInfo = CultureInfo.CurrentCulture.CompareInfo;
  39.         }
  40.        
  41.         public CaseInsensitiveComparer(CultureInfo culture)
  42.         {
  43.             if (culture == null) {
  44.                 throw new ArgumentNullException("culture");
  45.             }
  46.             m_compareInfo = culture.CompareInfo;
  47.         }
  48.        
  49.         public static CaseInsensitiveComparer Default {
  50.             get { return new CaseInsensitiveComparer(CultureInfo.CurrentCulture); }
  51.         }
  52.        
  53.         public static CaseInsensitiveComparer DefaultInvariant {
  54.             get {
  55.                 if (m_InvariantCaseInsensitiveComparer == null) {
  56.                     m_InvariantCaseInsensitiveComparer = new CaseInsensitiveComparer(CultureInfo.InvariantCulture);
  57.                 }
  58.                 return m_InvariantCaseInsensitiveComparer;
  59.             }
  60.         }
  61.        
  62.         // Behaves exactly like Comparer.Default.Compare except that the comparison is case insensitive
  63.         // Compares two Objects by calling CompareTo. If a ==
  64.         // b,0 is returned. If a implements
  65.         // IComparable, a.CompareTo(b) is returned. If a
  66.         // doesn't implement IComparable and b does,
  67.         // -(b.CompareTo(a)) is returned, otherwise an
  68.         // exception is thrown.
  69.         //
  70.         public int Compare(object a, object b)
  71.         {
  72.             string sa = a as string;
  73.             string sb = b as string;
  74.             if (sa != null && sb != null)
  75.                 return m_compareInfo.Compare(sa, sb, CompareOptions.IgnoreCase);
  76.             else
  77.                 return Comparer.Default.Compare(a, b);
  78.         }
  79.     }
  80. }

Developer Fusion