StructuralComparisons.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Collections / StructuralComparisons.cs / 1305376 / StructuralComparisons.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
// [....]
// 
 
using System;
 
namespace System.Collections {
    public static class StructuralComparisons {

        private static IComparer s_StructuralComparer; 
        private static IEqualityComparer s_StructuralEqualityComparer;
 
        public static IComparer StructuralComparer { 
            get {
                IComparer comparer = s_StructuralComparer; 
                if (comparer == null) {
                    comparer = new StructuralComparer();
                    s_StructuralComparer = comparer;
                } 
                return comparer;
            } 
        } 

        public static IEqualityComparer StructuralEqualityComparer { 
            get {
                IEqualityComparer comparer = s_StructuralEqualityComparer;
                if (comparer == null) {
                    comparer = new StructuralEqualityComparer(); 
                    s_StructuralEqualityComparer = comparer;
                } 
                return comparer; 
            }
        } 
    }

    [Serializable]
    internal class StructuralEqualityComparer : IEqualityComparer { 
        public new bool Equals(Object x, Object y) {
            if (x != null) { 
 
                IStructuralEquatable seObj = x as IStructuralEquatable;
 
                if (seObj != null){
                    return seObj.Equals(y, this);
                }
 
                if (y != null) {
                    return x.Equals(y); 
                } else { 
                    return false;
                } 
            }
            if (y != null) return false;
            return true;
        } 

        public int GetHashCode(Object obj) { 
            if (obj == null) return 0; 

            IStructuralEquatable seObj = obj as IStructuralEquatable; 

            if (seObj != null) {
                return seObj.GetHashCode(this);
            } 

            return obj.GetHashCode(); 
        } 
    }
 
    [Serializable]
    internal class StructuralComparer : IComparer {
        public int Compare(Object x, Object y) {
 
            if (x == null) return y == null ? 0 : -1;
            if (y == null) return 1; 
 
            IStructuralComparable scX = x as IStructuralComparable;
 
            if (scX != null) {
                return scX.CompareTo(y, this);
            }
 
            return Comparer.Default.Compare(x, y);
        } 
    } 

} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK