Dictionary & amp; lt; string, string & amp; gt; is a generic usage description.

Source: Internet
Author: User
Tags what interface

Dictionary <string, string> is a generic

It has a set function. Sometimes it can be viewed as an array.

Its structure is as follows: Dictionary <[key], [value]>

It is characteristic that the stored object needs to be saved to the generic type one-to-one correspondence with the [key] value.

Find the corresponding value through a certain [key]

For example:

// Instantiate the object

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

// Add object hitting

Dic. Add (1, "one ");

Dic. Add (2, "two ");

Dic. Add (3, "one ");

// Method for Extracting Elements

String a = dic [1];

String B = dic [2];

String c = dic [3];

// Keys 1, 2, and 3 correspond to "one", "two", and "one" respectively"

// In the code above, the values are assigned to a, B, and c respectively.

// Note that the key is equivalent to finding the unique identifier of the corresponding value, so it cannot be repeated.

// But the value can be repeated

If you still don't understand it, I will give you a simple example.

There is a tank of rice. You want to mark each grain with no duplicates, which is equivalent to "key". When you look for it, it will not be wrong, this is the role of this generic key, and meters can be the same. I mean, do you understand?

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

C # What interface is used to sort dictionary classes?

If you use. Net Framework 3.5, it's easy. Haha.

If not, write and sort by yourself.

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 orderby pair. Key select pair;

Foreach (KeyValuePair <int, string> pair in result)

{

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

}

Console. ReadKey ();

}

}

}

[Execution result]

Key: 1, Value: HaHa

Key: 2, Value: HiHi

Key: 3, Value: HeHe

Key: 4, Value: HuHu

Key: 5, Value: HoHo

The basic usage of Dictionary. Assume that

Requirement: now we want to import a batch of data. One of these data fields is a company field that already exists in our database. At present, we need to convert each company name into an ID before saving it to the database.

Analysis: When you export a record, you need to convert the company name into the company ID. This should not be queried every time, because it is too performance-consuming.

Solution: Read all company names and corresponding company IDs at one time at the business layer and store them in a Key-Value pair of Key and Value, then, as long as a company name is passed in, the company ID can be obtained, just like a dictionary. Yes, we can use the Dictionary to operate the data.

Example: The SetKeyValue () method reads company information from the database.

/// <Summary>
/// Define the Key as a string type and the Value as a Dictionary of the int type
/// </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>
/// Obtain the Value based on the specified Key row
/// </Summary>
Protected void GetKeyValue ()
{
Dictionary <string, int> myDictionary = SetKeyValue ();
// Obtain company 2 value from the test
Int directorValue = myDictionary ["company 2"];
Response. Write ("company 2's value is:" + directorValue. ToString ());
}

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.