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 :
definition dictionary<new dictionary<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 ( in Openwith.keys) {Console.WriteLine ("key = {0}", key);}
//Traverse valueforeach (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); }
Traverse dictionary foreach (keyvaluepair< in Openwith) {Console.WriteLine ("Key = {0}, Value = {1}< c11> ", kvp. Key, kvp. Value); }
Add the element that exists try { openwith.add ("txt"winword.exe"Catch" ( ArgumentException) {Console.WriteLine ("Anelement with Key = \" Txt\ "already exists.") ); }
Delete element openwith.remove ("doc"if (!openwith.containskey ("doc")) { Console.WriteLine ("Key \" Doc\ "is not found.") ); }
determine if the key exists if (Openwith.containskey ("bmp// {Console.WriteLine (" an element with Key = \ "Bmp\" exists. "); }
Parameters are other types
//parameter is other type dictionary<IntString[]> Othertype =new dictionary<int, string[]>(); Othertype.add (1, "1,11,111". Split (',')); Othertype.add (2, "2,22,222". Split (',')); Console.WriteLine (othertype[1][2]);
parameter is a custom type
First define the class
Class doucube { intstring
And then
//declaring and adding elements dictionary<IntDoucube> MyType =New dictionary<int, doucube> (); Span style= "color: #0000ff;" >for (int i = 9; I++doucube element = Doucube (); element. Code = i * 100 "http://www.doucube.com/ "+ i.tostring () + "; Mytype.add (i, Element); }
traversal element foreach (keyvaluepair<in MyType) {Console.WriteLine ("Index {0} code:{1} page: {2} "
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.
Introduction to dictionary dictionary classes in C #