System.Collections.Hashtable.Remove C# (CSharp) Method

Remove() public method

public Remove ( Object key ) : void
key Object
return void
        public virtual void Remove(Object key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
            }
            Contract.EndContractBlock();
            Debug.Assert(!_isWriterInProgress, "Race condition detected in usages of Hashtable - multiple threads appear to be writing to a Hashtable instance simultaneously!  Don't do that - use Hashtable.Synchronized.");

            uint seed;
            uint incr;
            // Assuming only one concurrent writer, write directly into buckets.
            uint hashcode = InitHash(key, _buckets.Length, out seed, out incr);
            int ntry = 0;

            bucket b;
            int bn = (int)(seed % (uint)_buckets.Length);  // bucketNumber
            do
            {
                b = _buckets[bn];
                if (((b.hash_coll & 0x7FFFFFFF) == hashcode) &&
                    KeyEquals(b.key, key))
                {
                    _isWriterInProgress = true;
                    // Clear hash_coll field, then key, then value
                    _buckets[bn].hash_coll &= unchecked((int)0x80000000);
                    if (_buckets[bn].hash_coll != 0)
                    {
                        _buckets[bn].key = _buckets;
                    }
                    else
                    {
                        _buckets[bn].key = null;
                    }
                    _buckets[bn].val = null;  // Free object references sooner & simplify ContainsValue.
                    _count--;
                    UpdateVersion();
                    _isWriterInProgress = false;
                    return;
                }
                bn = (int)(((long)bn + incr) % (uint)_buckets.Length);
            } while (b.hash_coll < 0 && ++ntry < _buckets.Length);
        }

Usage Example

Example #1
0
        public void NonEmptyDictionary()
        {
            Hashtable map = new Hashtable();

            map.Add(1, "one");
            map.Add(2, "two");
            map.Add(3, "three");

            ArrayList entryList = new ArrayList(DictionaryHelper.GetEntries(map));
            Assert.AreEqual(3, entryList.Count);

            DictionaryEntry first = Shift(entryList);
            Assert.AreEqual(first.Value, map[first.Key]);
            map.Remove(first.Key);

            DictionaryEntry second = Shift(entryList);
            Assert.AreEqual(second.Value, map[second.Key]);
            map.Remove(second.Key);

            DictionaryEntry third = Shift(entryList);
            Assert.AreEqual(third.Value, map[third.Key]);
            map.Remove(third.Key);

            Assert.AreEqual(0, map.Count);
            Assert.AreEqual(0, entryList.Count);
        }
All Usage Examples Of System.Collections.Hashtable::Remove