Build a reversible sort of generic dictionary class (8)--Implement IDictionary interface

Source: Internet
Author: User

8. Implement Idictionary<tkey, Tvalue> interface

Because the front implementation of the IDictionary interface, now realize Idictionary<tkey, Tvalue> also have no difficulty, divert.

First change the class declaration:

public class ReversibleSortedList<TKey, TValue> :IDictionary<TKey, TValue>
  IDictionary, IEnumerable<KeyValuePair<TKey, TValue>>, ICollection,
IEnumerable, ICollection<KeyValuePair<TKey, TValue>>

Then implement Icollection<keyvaluepair<tkey, Tvalue>> interface member:

bool Icollection&lt;keyvaluepair&lt;tkey, TVALUE&GT;&GT; IsReadOnly


  {


Get


     {


return false;


    }


  }


void Icollection&lt;keyvaluepair&lt;tkey, Tvalue&gt;&gt; ADD (


Keyvaluepair&lt;tkey, tvalue&gt; KeyValuePair)


  {


this. ADD (Keyvaluepair.key, Keyvaluepair.value);


  }


BOOL Icollection&lt;keyvaluepair&lt;tkey, TVALUE&GT;&GT; Contains (


Keyvaluepair&lt;tkey, tvalue&gt; KeyValuePair)


  {


int num1 = this. Indexofkey (Keyvaluepair.key);


if ((num1 &gt;= 0) &amp;&amp; Equalitycomparer&lt;tvalue&gt;. Default.equals (This.values[num1],


keyvaluepair.value))


    {


return true;


    }


return false;


  }


void Icollection&lt;keyvaluepair&lt;tkey, Tvalue&gt;&gt; CopyTo (


Keyvaluepair&lt;tkey, tvalue&gt;[] array, int arrayindex)


  {


if (array = = NULL)


     {


throw new ArgumentNullException ("array");


    }


if (arrayindex &lt; 0) | | (Arrayindex &gt; Array.) Length))


    {


throw New ArgumentOutOfRangeException (


"Arrayindex", "Need a non-negative number");


    }


if (array. Length-arrayindex) &lt; this. Count)


    {


throw new ArgumentException ("Arrayplusofftoosmall");


    }


for (int num1 = 0; Num1 &lt; this. Count; num1++)


     {


Keyvaluepair&lt;tkey, tvalue&gt; pair1;


Pair1 = new Keyvaluepair&lt;tkey, tvalue&gt; (


THIS.KEYS[NUM1], this.values[num1]);


Array[arrayindex + num1] = Pair1;


    }


  }


bool Icollection&lt;keyvaluepair&lt;tkey, TVALUE&GT;&GT; Remove (


Keyvaluepair&lt;tkey, tvalue&gt; KeyValuePair)


  {


int num1 = this. Indexofkey (Keyvaluepair.key);


if ((num1 &gt;= 0) &amp;&amp; Equalitycomparer&lt;tvalue&gt;. Default.equals (


This.values[num1], Keyvaluepair.value)


    {


this. RemoveAt (NUM1);


return true;


    }


return false;


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.