3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
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
{
19 IComparer comparer
= s_StructuralComparer
;
20 if (comparer
== null) {
21 comparer
= new StructuralComparer();
22 s_StructuralComparer
= comparer
;
28 public static IEqualityComparer StructuralEqualityComparer
{
30 IEqualityComparer comparer
= s_StructuralEqualityComparer
;
31 if (comparer
== null) {
32 comparer
= new StructuralEqualityComparer();
33 s_StructuralEqualityComparer
= comparer
;
41 internal class StructuralEqualityComparer
: IEqualityComparer
{
42 public new bool Equals(Object x
, Object y
) {
45 IStructuralEquatable seObj
= x
as IStructuralEquatable
;
48 return seObj
.Equals(y
, this);
57 if (y
!= null) return false;
61 public int GetHashCode(Object obj
) {
62 if (obj
== null) return 0;
64 IStructuralEquatable seObj
= obj
as IStructuralEquatable
;
67 return seObj
.GetHashCode(this);
70 return obj
.GetHashCode();
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
;
84 return scX
.CompareTo(y
, this);
87 return Comparer
.Default
.Compare(x
, y
);