/// <summary>
        /// Compares two property dictionaries for equivalence.  They are equal if each contains the same properties with the
        /// same values as the other, unequal otherwise.
        /// </summary>
        /// <param name="other">The dictionary to which this should be compared</param>
        /// <returns>True if they are equivalent, false otherwise.</returns>
        public bool Equals(CopyOnWritePropertyDictionary <T> other)
        {
            if (null == other)
            {
                return(false);
            }

            if (Object.ReferenceEquals(this, other))
            {
                return(true);
            }

            if (Count != other.Count)
            {
                return(false);
            }

            lock (_properties)
            {
                foreach (T leftProp in this)
                {
                    T rightProp = other[leftProp.Key];
                    if (rightProp == null || !EqualityComparer <T> .Default.Equals(leftProp, rightProp))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// Compares two property dictionaries for equivalence.  They are equal if each contains the same properties with the
        /// same values as the other, unequal otherwise.
        /// </summary>
        /// <param name="other">The dictionary to which this should be compared</param>
        /// <returns>True if they are equivalent, false otherwise.</returns>
        public bool Equals(CopyOnWritePropertyDictionary <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            // Copy both backing collections to locals
            ImmutableDictionary <string, T> thisBacking = _backing;
            ImmutableDictionary <string, T> thatBacking = other._backing;

            // If the backing collections are the same, we are equal.
            // Note that with this check, we intentionally avoid the common reference
            // comparison between 'this' and 'other'.
            if (ReferenceEquals(thisBacking, thatBacking))
            {
                return(true);
            }

            if (thisBacking.Count != thatBacking.Count)
            {
                return(false);
            }

            foreach (T thisProp in thisBacking.Values)
            {
                if (!thatBacking.TryGetValue(thisProp.Key, out T thatProp) ||
                    !EqualityComparer <T> .Default.Equals(thisProp, thatProp))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
 /// <summary>
 /// Cloning constructor, with deferred cloning semantics
 /// </summary>
 private CopyOnWritePropertyDictionary(CopyOnWritePropertyDictionary <T> that)
 {
     _properties = that._properties.Clone(); // copy on write!
 }
 /// <summary>
 /// Cloning constructor, with deferred cloning semantics
 /// </summary>
 private CopyOnWritePropertyDictionary(CopyOnWritePropertyDictionary <T> that)
 {
     _backing = that._backing;
 }