Introduction to the Dictionary class in C,

Source: Internet
Author: User

Introduction to the Dictionary class in C,

Description
Must contain the namespace System. Collection. Generic
Each element in Dictionary is a key-Value Pair (consisting of two elements: Key and value)
The key must be unique, and the value must not be unique.
Keys and values can be of any type (such as string, int, custom type, and so on)
The time for reading a value through a key is close to O (1)
The partial order between key-value pairs cannot be defined.

Usage:

// Define Dictionary <string, string> openWith = new Dictionary <string, string> ();

 

// Add the element openWith. add ("txt", "notepad.exe"); openWith. add ("bmp", "paint.exe"); openWith. add ("dib", "paint.exe"); openWith. add ("rtf", "wordpad.exe ");

 

// Set the value to Console. WriteLine ("For key = \" rtf \ ", value = {0}.", openWith ["rtf"]).

 

// Change the value of 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) ;}// traverses value, Second Method Dictionary <string, string>. valueCollection valueColl = openWith. values; foreach (string s in valueColl) {Console. writeLine ("Second Method, Value = {0}", s );}

 

// Traverse the dictionary foreach (KeyValuePair <string, string> kvp in openWith) {Console. writeLine ("Key = {0}, Value = {1}", kvp. key, kvp. value );}

 

 
// Add the existing element try {openWith. add ("txt", "winword.exe");} catch (ArgumentException) {Console. writeLine ("An element with Key = \" txt \ "already exists. ");}
 

 

// Delete the element openWith. Remove ("doc"); if (! OpenWith. ContainsKey ("doc") {Console. WriteLine ("Key \" doc \ "is not found .");}

 

// Determine if (openWith. containsKey ("bmp") // True {Console. writeLine ("An element with Key = \" bmp \ "exists. ");}

 

The parameter is of another type.

// The parameter is of another type: Dictionary <int, string []> OtherType = new Dictionary <int, string []> (); OtherType. add (1, "111 ". split (','); OtherType. add (2, "222 ". split (','); Console. writeLine (OtherType [1] [2]);

 

The parameter is of the 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;    } 

Then

 
// Declare and add the 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 );}
// Traverse the 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 attributes

Description
Comparer obtains 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 obtains the set of Keys that contain the Dictionary <TKey, TValue>.
Values obtains a set of Values in Dictionary <TKey, TValue>.

Common Methods
Description
Add adds the specified key and value to the dictionary.
Clear removes all keys and values from Dictionary <TKey, TValue>.
ContainsKey determines whether 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. (Inherited from Object .)
Finalize allows objects to try to release resources and perform other cleanup operations before "Garbage Collection. (Inherited from Object .)
GetEnumerator returns the iterator that cyclically accesses the Dictionary <TKey, TValue>.
GetHashCode is used as a hash function of a specific type. (Inherited from Object .)
GetObjectData implements the System. Runtime. Serialization. ISerializable interface, and returns the data required for serializing the Dictionary <TKey, TValue> instance.
GetType: Get the Type of the current instance. (Inherited from Object .)
MemberwiseClone creates a superficial copy of the current Object. (Inherited from Object .)
OnDeserialization implements the System. Runtime. Serialization. ISerializable interface and triggers a deserialization event after deserialization.
Remove removes the specified key value from Dictionary <TKey, TValue>.
Returns the string of the current object. (Inherited from Object .)
TryGetValue gets the value associated with the specified key.

Related Article

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.