The role and usage of Dictionary in C

  • 2020-11-18 06:24:04
  • OfStack

Dictionary < string, string > It's a generic type

It has a set function itself and sometimes you can think of it as an array

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

Its characteristic is that the deposit object is required to deposit the generic type corresponding to the [key] value 11

The corresponding value is found by a certain 1 fixed [key]

Here's an example:


// Instantiate object Dictionary<int, string> dic = new Dictionary<int, string>(); // Object dot add dic.Add(1, "one"); dic.Add(2, "two"); dic.Add(3, "one"); // Method of extracting elements string a = dic[1]; string b = dic[2]; string c = dic[3]; //1 , 2 , 3 Are the keys, which correspond to each other. one "" two "" one " // The above code assigns the values to a,b,c // Pay attention to , The key is equivalent to finding the unique value 1 Identification, so it cannot be repeated // But the values can be repeated

Let me give you one last example in case you don't understand

You have 1 cylinder of rice, you want to mark each one, you don't want to repeat it, it's the same thing as the "key" and when you look for the 11 you don't get it wrong, that's what the generic key does - and the meter can be one, you get the idea?

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

What interface does c# implement to sort the dictionary class

If you use.Net Framework 3.5, things are pretty straightforward. Ha ha.

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 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

Basic usage of Dictionary. if

Requirement: We now need to import a batch of data with one field called company that already exists in our database. At present, we need to convert each company name to ID before putting it into the database.

Analysis: Every time a record is made, the name of the company is changed to ID. This should not be used to query the database every time, because it is too expensive for the database performance.

Solution: In the business layer, all the company names and the corresponding company IDare read out once, and then stored in a key pair of Key and Value, and then realize that as long as the name of a company is passed in, you can get the corresponding company ID of this company, just like looking up dictionary 1. Yes, we can manipulate this data using the dictionary Dictionary.

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


/// <summary>
/// define Key for string Type, Value for int The type of 1 a Dictionary
/// </summary>
/// <returns></returns>
protected Dictionary<string, int> SetKeyValue()
{
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add(" The company 1", 1);
dic.Add(" The company 2", 2);
dic.Add(" The company 3", 3);
dic.Add(" The company 4", 4);
return dic;
}
/// <summary>
/// According to the specified Key Row to Value
/// </summary>
protected void GetKeyValue()
{
Dictionary<string, int> myDictionary = SetKeyValue();
// Test the company 2 The value of the
int directorValue = myDictionary[" The company 2"];
Response.Write(" The company 2 the value Is this: " + directorValue.ToString());
}


Related articles: