7 Common uses of dictionary generic collections in C #

Source: Internet
Author: User

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: the type of key is int, the type of value is string, for example

1. Create and initialize

The code is as follows:

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

2. Adding elements

The code is as follows:

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

3. Find elements by key

The code is as follows:

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

4. Traversing elements through KeyValuePair

The code is as follows:

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

5. Only the key Keys property is traversed

The code is as follows:

Dictionary<int,string>. KeyCollection Keycol=mydictionary.keys;
foreach (Intkeyinkeycol)
{
Console.WriteLine ("key = {0}", key);
}

6. Traverse only the Value Valus property

The code is as follows:

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

7. Remove the specified key value through the Remove method

The code is as follows:

Mydictionary.remove (1);
if (Mydictionary.containskey (1))
{
Console.WriteLine ("Key:{0},value:{1}", "1", mydictionary[1]);
}
Else
{
Console.WriteLine ("Non-existent key:1");
}

  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 a collection that contains the values in dictionary.
add: adds the specified key and value to the dictionary.
Clear: removes all keys and values from dictionary.
containskey: Determines whether dictionary contains the specified key.
containsvalue: Determines whether 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 dictionary.
ToString: Returns a String that represents the current object. (Inherit from Object.)
TryGetValue: Gets the value associated with the specified key.

Common use of dictionary generic collections in C # 7

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.