Description
Must contain a name space System.Collection.Generic
Each element in the dictionary is a key-value pair (consisting of two elements: a key and a value)
The key must be unique, and the value does not require a unique
Both the key and the value can be of any type (e.g. string, int, custom type, etc.)
The time to read a value through a key is close to O (1)
The partial order between key-value pairs can be undefined
How to use :
Define dictionary<string, string> openwith = new dictionary<string, string> (); add element Openwith.add ("TXT", "notepad.exe"); Openwith.add ("BMP", "Paint.exe"); Openwith.add ("Dib", "Paint.exe"); Openwith.add ("RTF", "Wordpad.exe"); Value Console.WriteLine ("For key = \" Rtf\ ", value = {0}.", openwith["RTF"]); Change the value openwith["RTF"] = "winword.exe"; Console.WriteLine ("For key = \" Rtf\ ", value = {0}.", openwith["RTF"]); Traverse key foreach (string key in Openwith.keys) {Console.WriteLine ("key = {0}", key); }//Traverse value foreach (string value in Openwith.values) {Console.WriteLine ("value = {0}", value); }//Traverse value, Second Method dictionary<string, String>. ValueCollection valuecoll = openwith.values; foreach (string s in Valuecoll) {Console.WriteLine ("Second Method, Value = {0}", s); }//Traversal dictionary foreach (keyvaluepair<string, string> kvp in Openwith) {Console.writelINE ("Key = {0}, Value = {1}", kvp. Key, kvp. Value); }//Add the element that exists try {openwith.add ("txt", "winword.exe"); } catch (ArgumentException) {Console.WriteLine ("an element with Key = \" Txt\ "already exists."); Delete element Openwith.remove ("Doc"); if (!openwith.containskey ("Doc")) {Console.WriteLine ("Key \" Doc\ "is not found."); The Judgment key exists if (Openwith.containskey ("BMP"))//True {Console.WriteLine ("an element with key = \" Bmp\ "exists.") ); }//parameter is other type Dictionary<int, string[]> othertype = new Dictionary<int, string[]> (); Othertype.add (1, "1,11,111". Split (', ')); Othertype.add (2, "2,22,222". Split (', ')); Console.WriteLine (Othertype[1][2]); The parameter is a custom type//first defines class Doucube {public int Code {get {return _code;} set {_code = value;}} private in T _code; public string Page {get {return _page;} set {_page = value;}} private String _page; }//Then declare and add element DicTionary<int, doucube> MyType = new Dictionary<int, doucube> (); for (int i = 1; I <= 9; i++) {doucube element = new Doucube (); Element. Code = i * 100; Element. page = "http://www.doucube.com/" + i.tostring () + ". html"; Mytype.add (i, Element); }//Traversal element foreach (Keyvaluepair<int, doucube> kvp in MyType) {Console.WriteLine ("Index {0} code:{1} Page:{2} ", kvp. Key, kvp. Value.code, kvp. Value.page); }
Common Properties
Name Description
Comparer gets the iequalitycomparer<t> used to determine whether the keys in the dictionary are equal.
Count gets the number of key/value pairs contained in Dictionary<tkey, tvalue>.
Item Gets or sets the value associated with the specified key.
Keys gets a collection that contains the keys in the Dictionary<tkey, tvalue>.
Values gets a collection that contains the values in Dictionary<tkey, tvalue>.
Common Methods
Name Description
Add adds the specified key and value to the dictionary.
Clear remove all keys and values from Dictionary<tkey, tvalue>.
ContainsKey determines whether the Dictionary<tkey, tvalue> contains the specified key.
Containsvalue determines whether Dictionary<tkey, Tvalue> contains a specific value.
Equals (object) determines whether the specified object is equal to the current object. (Inherit from Object.) )
Finalize allows an object to attempt to free resources and perform other cleanup operations before garbage collection is reclaimed. (Inherit from Object.) )
GetEnumerator Returns an enumerator that iterates through the Dictionary<tkey, tvalue>.
GetHashCode is used as a hash function for a particular type. (Inherit from Object.) )
The GetObjectData implements the System.Runtime.Serialization.ISerializable interface and returns the data required to serialize Dictionary<tkey, tvalue> the instance.
GetType gets the Type of the current instance. (Inherit from Object.) )
MemberwiseClone creates a shallow copy of the current Object. (Inherit from Object.) )
The ondeserialization implements the System.Runtime.Serialization.ISerializable interface and raises the deserialization event after deserialization is complete.
Remove removes the value of the specified key from Dictionary<tkey, tvalue>.
ToString returns a string representing the current object. (Inherit from Object.) )
TryGetValue gets the value associated with the specified key.