Пример #1
0
            public override bool Equals(object obj)
            {
                WeakRefCollection other = obj as WeakRefCollection;

                if (other == this)
                {
                    return(true);
                }

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

                for (int i = 0; i < Count; i++)
                {
                    if (this.InnerList[i] != other.InnerList[i])
                    {
                        if (this.InnerList[i] == null || !this.InnerList[i].Equals(other.InnerList[i]))
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            }
        public static void Contains()
        {
            var strings = new WeakRefCollection <string>();

            strings.Add("a");

            // comparer is used as expected
            Assert.True(strings.Contains("a", StringComparer.Ordinal));
            Assert.False(strings.Contains("A", StringComparer.Ordinal));
            Assert.True(strings.Contains("A", StringComparer.OrdinalIgnoreCase));

            // null check
            Assert.False(strings.Contains(null)); // obvious, since you can't add null

            // default comparer (EqualityComparer<T>.Default)
            var collection = new WeakRefCollection <AlwaysEqual>();
            var item1      = new AlwaysEqual();
            var item2      = new AlwaysEqual();

            collection.Add(item1);
            Assert.True(collection.Contains(item1));
            Assert.True(collection.Contains(item2));
            Assert.True(collection.Contains(item1, ReferenceEqualityComparer <AlwaysEqual> .Default));
            Assert.False(collection.Contains(item2, ReferenceEqualityComparer <AlwaysEqual> .Default));
        }
        public static void Remove()
        {
            var strings = new WeakRefCollection <string>();

            strings.Add("a");
            Assert.True(strings.Remove("a"));
            Assert.False(strings.Remove("a"));

            // comparer parameter is actually used
            strings.Add("b");
            Assert.True(strings.Remove("B", StringComparer.OrdinalIgnoreCase));
        }
        public static void Add()
        {
            var collection = new WeakRefCollection <int[]>();

            var array = new int[0];

            collection.Add(new WeakReference <int[]>(array));
            collection.Add(array); // can store multiple weak references to the same object

            Assert.Throws <ArgumentNullException>(() => collection.Add((WeakReference <int[]>)null));
            Assert.Throws <ArgumentNullException>(() => collection.Add((int[])null));
        }
Пример #5
0
        internal static void AddDeviceContext(DeviceContext dc)
        {
            if (activeDeviceContexts == null)
            {
                activeDeviceContexts = new WeakRefCollection
                {
                    RefCheckThreshold = 20
                };
            }

            if (!activeDeviceContexts.Contains(dc))
            {
                dc.Disposing += new EventHandler(OnDcDisposing);
                activeDeviceContexts.Add(dc);
            }
        }
Пример #6
0
 private static void Copy(WeakRefCollection sourceList, int sourceIndex, WeakRefCollection destinationList, int destinationIndex, int length)
 {
     if (sourceIndex < destinationIndex)
     {
         sourceIndex      += length;
         destinationIndex += length;
         while (length > 0)
         {
             destinationList.InnerList[--destinationIndex] = sourceList.InnerList[--sourceIndex];
             length--;
         }
     }
     else
     {
         while (length > 0)
         {
             destinationList.InnerList[destinationIndex++] = sourceList.InnerList[sourceIndex++];
             length--;
         }
     }
 }
Пример #7
0
        public override bool Equals(object obj)
        {
            WeakRefCollection weakRefCollection = obj as WeakRefCollection;

            if (weakRefCollection == this)
            {
                return(true);
            }
            if (weakRefCollection == null || Count != weakRefCollection.Count)
            {
                return(false);
            }
            for (int i = 0; i < Count; i++)
            {
                if (InnerList[i] != weakRefCollection.InnerList[i] && (InnerList[i] == null || !InnerList[i].Equals(weakRefCollection.InnerList[i])))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
 private static void Copy(WeakRefCollection sourceList, int sourceIndex, WeakRefCollection destinationList, int destinationIndex, int length)
 {
     if (sourceIndex < destinationIndex)
     {
         // We need to copy from the back forward to prevent overwrite if source and
         // destination lists are the same, so we need to flip the source/dest indices
         // to point at the end of the spans to be copied.
         sourceIndex      = sourceIndex + length;
         destinationIndex = destinationIndex + length;
         for (; length > 0; length--)
         {
             destinationList.InnerList[--destinationIndex] = sourceList.InnerList[--sourceIndex];
         }
     }
     else
     {
         for (; length > 0; length--)
         {
             destinationList.InnerList[destinationIndex++] = sourceList.InnerList[sourceIndex++];
         }
     }
 }
Пример #9
0
 private static void Copy(WeakRefCollection sourceList, int sourceIndex, WeakRefCollection destinationList, int destinationIndex, int length) {
     if (sourceIndex < destinationIndex) {
         // We need to copy from the back forward to prevent overwrite if source and
         // destination lists are the same, so we need to flip the source/dest indices
         // to point at the end of the spans to be copied.
         sourceIndex = sourceIndex + length;
         destinationIndex = destinationIndex + length;
         for (; length > 0; length--) {
             destinationList.InnerList[--destinationIndex] = sourceList.InnerList[--sourceIndex];
         }
     }
     else {
         for (; length > 0; length--) {
             destinationList.InnerList[destinationIndex++] = sourceList.InnerList[sourceIndex++];
         }
     }
 }