public bool Equals(ReferenceHolder <T> other)
        {
            var x = TryGetTarget();
            var y = other.TryGetTarget();

            if (x is null)
            {
                if (_weakReference is object)
                {
                    // 'x' is a weak reference that was collected. Verify 'y' is a collected weak reference with the
                    // same runtime hash code. This code path can fail in an edge case where the references to two
                    // different objects have both been collected, but the runtime hash codes for the objects were
                    // equal. Callers can ensure this case is not encountered by structuring equality checks such that
                    // at least one of the objects is alive at the time Equals is called.
                    return(y is null && other._weakReference is object && _hashCode == other._hashCode);
                }
                else
                {
                    // Null values are equal iff both were originally references to null.
                    return(y is null && other._weakReference is null);
                }
            }

            // Intentional reference equality check
            return(x == y);
        }
Exemple #2
0
        public bool Add(T value)
        {
            if (Contains(value))
            {
                return(false);
            }

            return(_values.Add(ReferenceHolder <T> .Weak(value)));
        }
Exemple #3
0
 public bool Contains(T value)
 {
     return(_values.Contains(ReferenceHolder <T> .Strong(value)));
 }