Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / collections / structuralcomparisons.cs
blobe5a1aa5563b34d4081d8606d108c3b4540dbb07f
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 //
9 using System;
11 namespace System.Collections {
12 public static class StructuralComparisons {
14 private static volatile IComparer s_StructuralComparer;
15 private static volatile IEqualityComparer s_StructuralEqualityComparer;
17 public static IComparer StructuralComparer {
18 get {
19 IComparer comparer = s_StructuralComparer;
20 if (comparer == null) {
21 comparer = new StructuralComparer();
22 s_StructuralComparer = comparer;
24 return comparer;
28 public static IEqualityComparer StructuralEqualityComparer {
29 get {
30 IEqualityComparer comparer = s_StructuralEqualityComparer;
31 if (comparer == null) {
32 comparer = new StructuralEqualityComparer();
33 s_StructuralEqualityComparer = comparer;
35 return comparer;
40 [Serializable]
41 internal class StructuralEqualityComparer : IEqualityComparer {
42 public new bool Equals(Object x, Object y) {
43 if (x != null) {
45 IStructuralEquatable seObj = x as IStructuralEquatable;
47 if (seObj != null){
48 return seObj.Equals(y, this);
51 if (y != null) {
52 return x.Equals(y);
53 } else {
54 return false;
57 if (y != null) return false;
58 return true;
61 public int GetHashCode(Object obj) {
62 if (obj == null) return 0;
64 IStructuralEquatable seObj = obj as IStructuralEquatable;
66 if (seObj != null) {
67 return seObj.GetHashCode(this);
70 return obj.GetHashCode();
74 [Serializable]
75 internal class StructuralComparer : IComparer {
76 public int Compare(Object x, Object y) {
78 if (x == null) return y == null ? 0 : -1;
79 if (y == null) return 1;
81 IStructuralComparable scX = x as IStructuralComparable;
83 if (scX != null) {
84 return scX.CompareTo(y, this);
87 return Comparer.Default.Compare(x, y);