How to use dictionary in C #

Source: Internet
Author: User

In C #, Dictionary provides quick part-time-based element search. You can use it 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.

Detailed 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.
Create and initialize a dictionary object
Dictionary mydictionary = New Dictionary ();
Add key
Static void main (string [] ARGs)
{
Dictionary d = New Dictionary ();
D. Add ("C #", 2 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
}
Search key
Static void main (string [] ARGs)
{
Dictionary d = New Dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
If (D. containskey ("VB") // true
{
Int P = d ["VB"];
Console. writeline (P );
}

If (D. containskey ("C "))
{
Int p1 = d ["C"];
Console. writeline (P1 );
}
}
Delete Element
Static void main (string [] ARGs)
{
Dictionary d = New Dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );

D. Remove ("C ");
D. Remove ("VB ");
}
Use containsvalue to find the existence of a value
Static void main (string [] ARGs)
{
Dictionary d = New Dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
If (D. containsvalue (1 ))
{
Console. writeline ("VB ");
}
If (D. containsvalue (2 ))
{
Console. writeline ("C #");
}
If (D. containsvalue (0 ))
{
Console. writeline ("C ");
}
If (D. containsvalue (-1 ))
{
Console. writeline ("C ++ ");
}
}
Keynotfoundexception
If you try to read a key that does not exist in the dictionary, you will get a keynotfoundexception. Before reading a key, you must use containkey to check whether the key exists in the dictionary.

INT-based dictionary
Static void main (string [] ARGs)
{
Dictionary d = New Dictionary ();
D. Add (1000, "planet ");
D. Add (2000, "Stars ");
// Lookup the int in the dictionary.
If (D. containskey (1000 ))
{
Console. writeline (true );
}
Console. Readline ();
}
Sorteddictionary
In the sort dictionary, the dictionary must be sorted when elements are added, so the insertion speed is slower. However, because the elements are stored in an orderly manner, binary search can be used for element search, which is more efficient.

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.