mscorlib(4.0.0.0) API with additions
Comparer.cs
4 using System.Security;
5 
6 namespace System.Collections
7 {
10  [ComVisible(true)]
11  public sealed class Comparer : IComparer, ISerializable
12  {
13  private CompareInfo m_compareInfo;
14 
16  public static readonly Comparer Default = new Comparer(CultureInfo.CurrentCulture);
17 
20 
21  private const string CompareInfoName = "CompareInfo";
22 
23  private Comparer()
24  {
25  m_compareInfo = null;
26  }
27 
32  public Comparer(CultureInfo culture)
33  {
34  if (culture == null)
35  {
36  throw new ArgumentNullException("culture");
37  }
38  m_compareInfo = culture.CompareInfo;
39  }
40 
41  private Comparer(SerializationInfo info, StreamingContext context)
42  {
43  m_compareInfo = null;
44  SerializationInfoEnumerator enumerator = info.GetEnumerator();
45  while (enumerator.MoveNext())
46  {
47  string name = enumerator.Name;
48  if (name == "CompareInfo")
49  {
50  m_compareInfo = (CompareInfo)info.GetValue("CompareInfo", typeof(CompareInfo));
51  }
52  }
53  }
54 
64  public int Compare(object a, object b)
65  {
66  if (a == b)
67  {
68  return 0;
69  }
70  if (a == null)
71  {
72  return -1;
73  }
74  if (b == null)
75  {
76  return 1;
77  }
78  if (m_compareInfo != null)
79  {
80  string text = a as string;
81  string text2 = b as string;
82  if (text != null && text2 != null)
83  {
84  return m_compareInfo.Compare(text, text2);
85  }
86  }
87  IComparable comparable = a as IComparable;
88  if (comparable != null)
89  {
90  return comparable.CompareTo(b);
91  }
92  IComparable comparable2 = b as IComparable;
93  if (comparable2 != null)
94  {
95  return -comparable2.CompareTo(a);
96  }
97  throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
98  }
99 
105  [SecurityCritical]
107  {
108  if (info == null)
109  {
110  throw new ArgumentNullException("info");
111  }
112  if (m_compareInfo != null)
113  {
114  info.AddValue("CompareInfo", m_compareInfo);
115  }
116  }
117  }
118 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
Compares two objects for equivalence, where string comparisons are case-sensitive.
Definition: Comparer.cs:11
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual CompareInfo? CompareInfo
Gets the T:System.Globalization.CompareInfo that defines how to compare strings for the culture.
Definition: CultureInfo.cs:447
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo object with the data required for serial...
Definition: Comparer.cs:106
int Compare(object a, object b)
Performs a case-sensitive comparison of two objects of the same type and returns a value indicating w...
Definition: Comparer.cs:64
Definition: __Canon.cs:3
Comparer(CultureInfo culture)
Initializes a new instance of the T:System.Collections.Comparer class using the specified T:System....
Definition: Comparer.cs:32
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static readonly Comparer Default
Represents an instance of T:System.Collections.Comparer that is associated with the P:System....
Definition: Comparer.cs:16
Implements a set of methods for culture-sensitive string comparisons.
Definition: CompareInfo.cs:13
Exposes a method that compares two objects.
Definition: IComparer.cs:8
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
Specifies that the class can be serialized.
string Name
Gets the name for the item currently being examined.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
static readonly Comparer DefaultInvariant
Represents an instance of T:System.Collections.Comparer that is associated with P:System....
Definition: Comparer.cs:19
bool MoveNext()
Updates the enumerator to the next item.