Code for System.Collections.Generic.Dictionary<TKey,TValue>.Insert(TKey, TValue, Boolean)

Try compare this to System.Collections.Hashtable.Insert .  They are similar in many ways.

 

private void Insert(TKey key, TValue value, bool add)
{
    int index;
    if (key == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    }
    if (this.buckets == null)
    {
        this.Initialize(0);
    }
    int num = this.comparer.GetHashCode(key) & 0x7fffffff;
    for (int i = this.buckets[num % this.buckets.Length]; i >= 0; i = this.entries[i].next)
    {
        if ((this.entries[i].hashCode == num) && this.comparer.Equals(this.entries[i].key, key))
        {
            if (add)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
            }
            this.entries[i].value = value;
            this.version++;
            return;
        }
    }
    if (this.freeCount > 0)
    {
        index = this.freeList;
        this.freeList = this.entries[index].next;
        this.freeCount--;
    }
    else
    {
        if (this.count == this.entries.Length)
        {
            this.Resize();
        }
        index = this.count;
        this.count++;
    }
    int num4 = num % this.buckets.Length;
    this.entries[index].hashCode = num;
    this.entries[index].next = this.buckets[num4];
    this.entries[index].key = key;
    this.entries[index].value = value;
    this.buckets[num4] = index;
    this.version++;
}

Other Interesting Posts

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


2 + = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>