mscorlib(4.0.0.0) API with additions
StringComparer.cs
1 using System.Collections;
5 
6 namespace System
7 {
10  [ComVisible(true)]
11  [__DynamicallyInvokable]
12  public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
13  {
14  private static readonly StringComparer _invariantCulture = new CultureAwareComparer(CultureInfo.InvariantCulture, ignoreCase: false);
15 
16  private static readonly StringComparer _invariantCultureIgnoreCase = new CultureAwareComparer(CultureInfo.InvariantCulture, ignoreCase: true);
17 
18  private static readonly StringComparer _ordinal = new OrdinalComparer(ignoreCase: false);
19 
20  private static readonly StringComparer _ordinalIgnoreCase = new OrdinalComparer(ignoreCase: true);
21 
24  public static StringComparer InvariantCulture => _invariantCulture;
25 
28  public static StringComparer InvariantCultureIgnoreCase => _invariantCultureIgnoreCase;
29 
32  [__DynamicallyInvokable]
33  public static StringComparer CurrentCulture
34  {
35  [__DynamicallyInvokable]
36  get
37  {
38  return new CultureAwareComparer(CultureInfo.CurrentCulture, ignoreCase: false);
39  }
40  }
41 
44  [__DynamicallyInvokable]
46  {
47  [__DynamicallyInvokable]
48  get
49  {
50  return new CultureAwareComparer(CultureInfo.CurrentCulture, ignoreCase: true);
51  }
52  }
53 
56  [__DynamicallyInvokable]
57  public static StringComparer Ordinal
58  {
59  [__DynamicallyInvokable]
60  get
61  {
62  return _ordinal;
63  }
64  }
65 
68  [__DynamicallyInvokable]
69  public static StringComparer OrdinalIgnoreCase
70  {
71  [__DynamicallyInvokable]
72  get
73  {
74  return _ordinalIgnoreCase;
75  }
76  }
77 
85  [__DynamicallyInvokable]
86  public static StringComparer Create(CultureInfo culture, bool ignoreCase)
87  {
88  if (culture == null)
89  {
90  throw new ArgumentNullException("culture");
91  }
92  return new CultureAwareComparer(culture, ignoreCase);
93  }
94 
106  public int Compare(object x, object y)
107  {
108  if (x == y)
109  {
110  return 0;
111  }
112  if (x == null)
113  {
114  return -1;
115  }
116  if (y == null)
117  {
118  return 1;
119  }
120  string text = x as string;
121  if (text != null)
122  {
123  string text2 = y as string;
124  if (text2 != null)
125  {
126  return Compare(text, text2);
127  }
128  }
129  IComparable comparable = x as IComparable;
130  if (comparable != null)
131  {
132  return comparable.CompareTo(y);
133  }
134  throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
135  }
136 
142  public new bool Equals(object x, object y)
143  {
144  if (x == y)
145  {
146  return true;
147  }
148  if (x == null || y == null)
149  {
150  return false;
151  }
152  string text = x as string;
153  if (text != null)
154  {
155  string text2 = y as string;
156  if (text2 != null)
157  {
158  return Equals(text, text2);
159  }
160  }
161  return x.Equals(y);
162  }
163 
170  public int GetHashCode(object obj)
171  {
172  if (obj == null)
173  {
174  throw new ArgumentNullException("obj");
175  }
176  string text = obj as string;
177  if (text != null)
178  {
179  return GetHashCode(text);
180  }
181  return obj.GetHashCode();
182  }
183 
194  [__DynamicallyInvokable]
195  public abstract int Compare(string x, string y);
196 
202  [__DynamicallyInvokable]
203  public abstract bool Equals(string x, string y);
204 
211  [__DynamicallyInvokable]
212  public abstract int GetHashCode(string obj);
213 
215  [__DynamicallyInvokable]
216  protected StringComparer()
217  {
218  }
219  }
220 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
int Compare(object x, object y)
When overridden in a derived class, compares two objects and returns an indication of their relative ...
static StringComparer InvariantCulture
Gets a T:System.StringComparer object that performs a case-sensitive string comparison using the word...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
int GetHashCode(object obj)
When overridden in a derived class, gets the hash code for the specified object.
Definition: __Canon.cs:3
new bool Equals(object x, object y)
When overridden in a derived class, indicates whether two objects are equal.
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
static StringComparer Ordinal
Gets a T:System.StringComparer object that performs a case-sensitive ordinal string comparison.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Defines methods to support the comparison of objects for equality.
Exposes a method that compares two objects.
Definition: IComparer.cs:8
static StringComparer Create(CultureInfo culture, bool ignoreCase)
Creates a T:System.StringComparer object that compares strings according to the rules of a specified ...
static StringComparer InvariantCultureIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive string comparison using the wo...
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.
static StringComparer CurrentCulture
Gets a T:System.StringComparer object that performs a case-sensitive string comparison using the word...
Specifies that the class can be serialized.
static StringComparer OrdinalIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive ordinal string comparison.
static StringComparer CurrentCultureIgnoreCase
Gets a T:System.StringComparer object that performs case-insensitive string comparisons using the wor...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
StringComparer()
Initializes a new instance of the T:System.StringComparer class.