C # Dictionary

1. Use the Dictionary reason

    Under normal circumstances, we may be from an int array by index number or list the collection of data needed by the query. However, if the situation is slightly more complicated: non int index data type such as string or other type of how to operate it. This time we can use the dictionary.

2. What is the dictionary

    As the name suggests, it is a dictionary so that we can query the data structure of type-specific data by index number.

    Keyword DIctionary

3. Usage and Precautions

   (1) C # is Dictionary <Tkey, TValue> class maintains two arrays to implement this function in the interior. Receiving a key array from which the keys mapped, are mapped to other values ​​received value. When the key is inserted in the Dictionary <Tkey, TValue> set / value pairs, which are automatically recorded

    Keys and associated values ​​which, allowing developers to quickly and easily get the value with the specified key.

   (2) C # is Dictionary <Tkey, TValue> set can not contain duplicate keys. Call the Add method to add key array of existing bonds will throw an exception. However, if you add a button using square bracket notation (similar to the array element assignment) / value pairs, do not worry exception - if the key has been

    It existed, its value will be overwritten with the new value. The method can be used to test ContainKey Dictionary <Tkey, TValue> whether the set already contains a specific key.
   (3) Dictionary <Tkey, TValue > uses a sparse set of internal data structure, available in a large number of memory when the most efficient. With the insertion of additional elements, Dictionary <Tkey, TValue> collection may quickly consume large amounts of memory.
   (4) traversing Dictionary <Tkey, TValue> with foreach returns a collection of KeyValuePair <Tkey, TValue>. Copy the key and the value contains the configuration data item, by mosquito Key and Value attributes of each element. Element is read-only, can not be modified <TValue Tkey,> Dictionary data set with them.

4. Examples

      Dictionary<string,string> students=new Dictionary<string,string>();

      insert:

           students.Add ("S001","张三");

           students.Add ("S002","李四");

           students["S003"]="王五";

     delete:

           Students. the Remove ( "S000"); // delete can be built

     modify:

         Just copy it Students [ "S003"] = "Wang five" ;

   Inquire:

        foreach(KeyValuePair<string,string> stu in students)  //查询所Keys 和Value
              Debug.Log ("Key:"+stu.Key+"  Name:"+stu.Value);

        foreach (string value in students.Values)
              Debug.Log (value);

       foreach (string key in students.Keys)
             Debug.Log (key);

Guess you like

Origin www.cnblogs.com/xingyuanzier/p/12031818.html