mscorlib(4.0.0.0) API with additions
EqualityComparer.cs
2 using System.Security;
3 
5 {
9  [TypeDependency("System.Collections.Generic.ObjectEqualityComparer`1")]
10  [__DynamicallyInvokable]
12  {
13  private static volatile EqualityComparer<T> defaultComparer;
14 
17  [__DynamicallyInvokable]
18  public static EqualityComparer<T> Default
19  {
20  [__DynamicallyInvokable]
21  get
22  {
23  EqualityComparer<T> equalityComparer = defaultComparer;
24  if (equalityComparer == null)
25  {
26  equalityComparer = (defaultComparer = CreateComparer());
27  }
28  return equalityComparer;
29  }
30  }
31 
32  [SecuritySafeCritical]
33  private static EqualityComparer<T> CreateComparer()
34  {
35  RuntimeType runtimeType = (RuntimeType)typeof(T);
36  if (runtimeType == typeof(byte))
37  {
38  return (EqualityComparer<T>)new ByteEqualityComparer();
39  }
40  if (typeof(IEquatable<T>).IsAssignableFrom(runtimeType))
41  {
42  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer<int>), runtimeType);
43  }
44  if (runtimeType.IsGenericType && runtimeType.GetGenericTypeDefinition() == typeof(Nullable<>))
45  {
46  RuntimeType runtimeType2 = (RuntimeType)runtimeType.GetGenericArguments()[0];
47  if (typeof(IEquatable<>).MakeGenericType(runtimeType2).IsAssignableFrom(runtimeType2))
48  {
49  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer<int>), runtimeType2);
50  }
51  }
52  if (runtimeType.IsEnum)
53  {
54  switch (Type.GetTypeCode(Enum.GetUnderlyingType(runtimeType)))
55  {
56  case TypeCode.Int16:
57  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer<short>), runtimeType);
58  case TypeCode.SByte:
59  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer<sbyte>), runtimeType);
60  case TypeCode.Byte:
61  case TypeCode.UInt16:
62  case TypeCode.Int32:
63  case TypeCode.UInt32:
64  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer<int>), runtimeType);
65  case TypeCode.Int64:
66  case TypeCode.UInt64:
67  return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer<long>), runtimeType);
68  }
69  }
70  return new ObjectEqualityComparer<T>();
71  }
72 
78  [__DynamicallyInvokable]
79  public abstract bool Equals(T x, T y);
80 
85  [__DynamicallyInvokable]
86  public abstract int GetHashCode(T obj);
87 
88  internal virtual int IndexOf(T[] array, T value, int startIndex, int count)
89  {
90  int num = startIndex + count;
91  for (int i = startIndex; i < num; i++)
92  {
93  if (Equals(array[i], value))
94  {
95  return i;
96  }
97  }
98  return -1;
99  }
100 
101  internal virtual int LastIndexOf(T[] array, T value, int startIndex, int count)
102  {
103  int num = startIndex - count + 1;
104  for (int num2 = startIndex; num2 >= num; num2--)
105  {
106  if (Equals(array[num2], value))
107  {
108  return num2;
109  }
110  }
111  return -1;
112  }
113 
119  [__DynamicallyInvokable]
120  int IEqualityComparer.GetHashCode(object obj)
121  {
122  if (obj == null)
123  {
124  return 0;
125  }
126  if (obj is T)
127  {
128  return GetHashCode((T)obj);
129  }
130  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison);
131  return 0;
132  }
133 
141  [__DynamicallyInvokable]
142  bool IEqualityComparer.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  if (x is T && y is T)
153  {
154  return Equals((T)x, (T)y);
155  }
156  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison);
157  return false;
158  }
159 
161  [__DynamicallyInvokable]
162  protected EqualityComparer()
163  {
164  }
165  }
166 }
Provides a base class for implementations of the T:System.Collections.Generic.IEqualityComparer`1 gen...
Defines methods to support the comparison of objects for equality.
TypeCode
Specifies the type of an object.
Definition: TypeCode.cs:9
Definition: __Canon.cs:3
Represents a type using an internal metadata token.
Represents a value type that can be assigned null.
Definition: Nullable.cs:12
static EqualityComparer< T > Default
Returns a default equality comparer for the type specified by the generic argument.
EqualityComparer()
Initializes a new instance of the T:System.Collections.Generic.EqualityComparer`1 class.
Attribute can be applied to an enumeration.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
Specifies that the class can be serialized.
abstract int GetHashCode(T obj)
When overridden in a derived class, serves as a hash function for the specified object for hashing al...
abstract bool Equals(T x, T y)
When overridden in a derived class, determines whether two objects of type T are equal.