Use of dictionary in C # [reprint]

Source: Internet
Author: User

In C #, Dictionary provides quick, part-based element lookups. His structure is this: Dictionary<[key], [value]>, you can use it when you have a lot of elements. It is contained in the System.Collections.Generic name space. Before you use it, you must declare its key type and value type.

To use the dictionary collection, you need to import a C # generic namespace

System.Collections.Generic (assembly: mscorlib)

Description of the Dictionary

1, from a set of keys (key) to a set of values (value) of the mapping, each add-on is a value and its associated key composition

2. Any key must be unique

3, the key cannot be null reference null (Nothing in VB), if the value is a reference type, you can be a null value

4, key and value can be any type (String,int,custom class, etc.)

Dictionary common usage: type of key is int, type of value is string for example

1. Create and initialize

dictionary<int,string>myDictionary=newDictionary<int,string> () ;

2. Adding elements

Mydictionary.add (1,"C #"); Mydictionary.add (2," C + + " ); Mydictionary.add (3,"asp"); Mydictionary.add (  4,"MVC");

3. Find elements by key

if (Mydictionary.containskey (1)) {Console.WriteLine ("key:{0},value:{1}") ,"1", mydictionary[1]);}

4. Traversing elements through KeyValuePair

foreach (keyvaluepair<int,string in mydictionary) ... {Console.WriteLine ("Key = {0}, Value = {1}", kvp. Key, kvp. Value);}

5. only the key keys property is traversed

dictionary<int,string;. KeyCollection keycol=Mydictionary.keys; foreach (Intkeyinkeycol) ... {Console.WriteLine ("key = {0}", key);}

6. Traverse only the Value Valus property

dictionary<int,string;. ValueCollection valuecol=mydictionary.values; foreach (Stringvalueinvaluecol) ... {Console.WriteLine ("value = {0}", Value);}

7. Remove the specified key value through the Remove method

Mydictionary.remove (1); if (Mydictionary.containskey (1)) ... {Console.WriteLine ("key:{0},value:{1}","1", mydictionary[1]);} Else {Console.WriteLine (" key:1 not present");  }

Description of other common properties and methods:

Comparer: Gets the iequalitycomparer used to determine whether the keys in the dictionary are equal.

Count: Gets the number of key/value pairs contained in the dictionary.

Item: Gets or sets the value associated with the specified key.

Keys: Gets the collection that contains the keys in the dictionary.

Values: Gets the collection that contains the values in the dictionary.

Add: Adds the specified key and value to the dictionary.

Clear: Removes all keys and values from the dictionary.

ContainsKey: Determines whether the dictionary contains the specified key.

Containsvalue: Determines whether the dictionary contains a specific value.

GetEnumerator: Returns an enumerator that iterates through the dictionary.

GetType: Gets the Type of the current instance. (Inherit from Object.) )

Remove: Removes the value of the specified key from the dictionary.

ToString: Returns a String that represents the current object. (Inherit from Object.) )

TryGetValue: Gets the value associated with the specified key.

Use of dictionary in C # [reprint]

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.