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
Sort by using LINQ
1. Sort by Value dictionary
private Void dictonarysort (dictionary<string, int> DiC)
{
var dicsort = FROM&NBSP;OBJDIC&NBSP; IN&NBSP;DIC&NBSP; orderby objdic.value descending select objdic;
If value is a collection of objects containing multiple objects (or JSON arrays) Var dicsort = from objdic in dic orderby objdic.value["... (JSON attribute name) "] descending select objdic;
foreach (keyvaluepair<string, int> kvp in dicsort)
Response.Write (KVP. Key + ":" + kvp. Value + "<br/>");
}
Sort Result:
Index.html:50
Online.aspx:22
News.aspx:18
Product.html:13
Aboutus.html:4
The above code is in descending order (in reverse order), if you want to sort in ascending (sequential), you just need to dicsort the variable to the right of the descending to remove.
2. C # Dictionary key sort
If you want to sort by Key, just change the objdic.value on the right side of the variable dicsort to Objdic.key.
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); }
Traverse 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.");
Determine if the key exists if (Openwith.containskey ("BMP"))//True { Console.WriteLine ("an element with key = \" Bmp\ " exists. ");
Parameters are other types
The parameters are other types 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]);
parameter is a custom type
First define the class
Class Doucube {public int Code {get {return _code;} set {_code = value;}} private int _code; public string Page {get {return _page;} set {_page = value;}} private String _page;
And 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 *; 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.
Common techniques for C # dictionaries