Usage of dictionary in C #

Source: Internet
Author: User

In C #, Dictionary provides quick part-time-based element search. Its structure is as follows: dictionary <[Key], [value]>, which can be used when you have many elements. It is included in the system. Collections. Generic namespace. Before using it, you must declare its key type and value type.

Method/step
  1.  To use the dictionary set, you must import the C # Generic namespace.

    System. Collections. Generic (Assembly: mscorlib)

  2. Dictionary description

    1. ing from a group of keys to a group of values. Each add item is composed of a value and its associated keys.

    2. Any key must be unique.

    3. The key cannot be null reference null (nothing in VB). If the value is of the reference type, it can be null.

    4. Key and value can be of any type (string, Int, custom class, etc)

  3. DictionaryCommon usage: the Key type is int, and the value type is string.

    1. Create and initialize

     Dictionary <int, string> mydictionary = newdictionary <int, string> ();

     

    2. Add Elements

    Mydictionary. Add (1, "C #");

    Mydictionary. Add (2, "C ++ ");

    Mydictionary. Add (3, "ASP. NET ");

    Mydictionary. Add (4, "MVC ");

     

    3. Search for elements by key

    If (mydictionary. containskey (1 ))

    {

    Console. writeline ("key: {0}, value: {1}", "1", mydictionary [1]);

    }

     

    4. Traverse elements through keyvaluepair

    Foreach (keyvaluepair <int, string> kVp in mydictionary)

    ...{

    Console. writeline ("Key = {0}, value = {1}", kVp. Key, kVp. value );

    }

     

    5,Only the keys attribute of the traversal key

    Dictionary <int, string>. keycollection keycol = mydictionary. Keys;

    Foreach (intkeyinkeycol)

    ...{

    Console. writeline ("Key = {0}", key );

    }

     

    6. Only the valus attribute is traversed.

    Dictionary <int, string>. valuecollection valuecol = mydictionary. values;

    Foreach (stringvalueinvaluecol)

    ...{

    Console. writeline ("value = {0}", value );

    }

     

    7. Remove the specified key value using the Remove Method.

    Mydictionary. Remove (1 );

    If (mydictionary. containskey (1 ))

    ...{

    Console. writeline ("key: {0}, value: {1}", "1", mydictionary [1]);

    }

    Else

    {

    Console. writeline ("key not found: 1 ");

    }

  4. Other common attributes and methods:

     

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

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

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

    Keys: gets a set of keys that contain a dictionary.

    Values: gets a set that contains values in a dictionary.

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

    Clear: removes all keys and values from dictionary.

    Containskey: determines whether a dictionary contains the specified key.

    Containsvalue: determines whether a dictionary contains a specific value.

    Getenumerator: returns the number of enumerations of the cyclically accessed dictionary.

    GetType: Get the type of the current instance. (Inherit from object .)

    Remove: remove the specified key value from dictionary.

    Tostring: returns the string of the current object. (Inherit 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.