Use and use of dictionary in C #

Source: Internet
Author: User
Tags what interface

Dictionary<string, string> is a generic

He has a set of functions that can sometimes be seen as an array.

His structure is this: Dictionary<[key], [value]>

His feature is that the deposit object is required to be stored with the [key] value one by one in the generic

To find the corresponding value by a certain [key]

As an example:

Instantiating an Object

Dictionary<int, string> dic = new Dictionary<int, string> ();

Object dot Add

Dic. ADD (1, "one");

Dic. ADD (2, "both");

Dic. ADD (3, "one");

Methods for extracting elements

String a = Dic[1];

String b = dic[2];

String c = dic[3];

1, 2, 3 are keys, respectively, corresponding to "one" "Two" "a"

The above code assigns the value to the A,b,c

Note that the key is equivalent to finding a unique identifier for the corresponding value, so you cannot repeat

But the values can be repeated

If you still don't understand, I'll give you a pop-up example at the end.

There is a cylinder of rice, you want to mark on every grain, do not repeat, the equivalent of "key" when you look for the one by one correspondence will not be wrong, this is the key of the generic-function, and meters can be the same, I mean you understand it?

-------------------------------------------------------------------------

C # What interface is used to sort the dictionary class

If you use the. Net Framework 3.5, things are easy. Oh.

If not, write your own sort.

Using System;

Using System.Collections.Generic;

Using System.Text;

Using System.Linq;

Namespace Dictionarysorting

{

Class Program

{

static void Main (string[] args)

{

Dictionary<int, string> dic = new Dictionary<int, string> ();

Dic. ADD (1, "HaHa");

Dic. ADD (5, "HoHo");

Dic. ADD (3, "HeHe");

Dic. ADD (2, "Hihi");

Dic. ADD (4, "Huhu");

var result = from pair in DIC by the pair. Key Select pair;

foreach (Keyvaluepair<int, string> pair in result)

{

Console.WriteLine ("Key:{0}, Value:{1}", pair. Key, pair. Value);

}

Console.readkey ();

}

}

}

"Execution Results"

Key:1, Value:haha

Key:2, Value:hihi

Key:3, Value:hehe

Key:4, Value:huhu

Key:5, Value:hoho

Basic usage of dictionary. If

Requirements: Now to import a batch of data, which has a field called company is already in our database, we need to convert each company name into an ID before being deposited into the database.

Analysis: Each lead a record, when the company's name to the company's ID, this should not be queried every time the database, because it is too expensive database performance.

Solution: In the business layer first to all the company name and the corresponding company ID read out once, and then deposited into a key and value of the value of the pair, and then realize as long as the name of a company passed in, you can get the company's corresponding company ID, like look up a dictionary. Yes, we can manipulate the data using the dictionary dictionary.

Example: the SetKeyValue () method corresponds to reading the company information from the database.

<summary>
Defines the key as a string type, and value is a dictionary of type int
</summary>
<returns></returns>
Protected Dictionary<string, int> SetKeyValue ()
{
dictionary<string, int> dic = new dictionary<string, int> ();
Dic. ADD ("Company 1", 1);
Dic. ADD ("Company 2", 2);
Dic. ADD ("Company 3", 3);
Dic. ADD ("Company 4", 4);
return dic;
}


<summary>
Gets the value according to the specified key line
</summary>
protected void Getkeyvalue ()
{
dictionary<string, int> mydictionary = SetKeyValue ();
The test gets the value of company 2
int directorvalue = mydictionary["Company 2"];
Response.Write ("The value of Company 2 is:" + directorvalue.tostring ());
}

Use and use of dictionary in C #

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.