Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The use of the C#Dictionary


May 12, 2021 C#



The primary purpose of Dictionary in C# is to provide fast part-time-based element lookups. T he structure of The Dictionary is generally like this: Dictionary-lt; B efore you can use Dictionary, you must declare its key type and value type.


To make it easier and more effective to learn the C-tutorial, we recommend that you take the C-micro-course


To use the Dictionary collection, you need to import the C-generic namespace

System.Collections.Generic (Assembly: mscorlib)


Description of Dictionary

1, from a set of keys (Key) to a set of values (Value) mapping, each addition is composed of a value and its associated keys

2. Any key must be unique

3, the key can not be an empty reference null (nothing in VB), if the value is a reference type, you can be an empty value

4, Key and Value can be any type (string, int, custom class, etc.)


Dictionary is commonly used: the type of key is int, the type of value is stringing, for example

1, creation and initialization

Dictionary<int,string>myDictionary=newDictionary<int,string>();

2, add elements

myDictionary.Add(1,"C#");
myDictionary.Add(2,"C++");
myDictionary.Add(3,"ASP.NET");
myDictionary.Add(4,"MVC");

3, find elements through Key

if(myDictionary.ContainsKey(1))
{
Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
 }

4, traversing elements through KeyValuePair

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

5, only traversing the key Keys property

Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys;
foreach(intkeyinkeyCol)
...{
Console.WriteLine("Key = {0}", key);
}

6, only traversing the value Valus properties

Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values;
foreach(stringvalueinvalueCol)
...{
Console.WriteLine("Value = {0}", value);
}

7, through the Remove method to remove the specified key value

myDictionary.Remove(1);
if(myDictionary.ContainsKey(1))
...{
  Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
}
else
{
Console.WriteLine("不存在 Key : 1"); 
 }


Description of other common properties and methods:

Comparer Gets the IEqualityComparer that determines whether the keys in the dictionary are equal
Count
Gets the number of key/value pairs contained in Dictionary
Item Gets or sets the value associated with the specified key
Keys Gets a collection that contains keys in Dictionary
Values Gets a collection that contains values in Dictionary
Add Add the specified keys and values to the dictionary
Clear Remove all keys and values from Dictionary
ContainsKey Determines whether Dictionary contains the specified key
ContainsValue Determines whether Dictionary contains a specific value
GetEnumerator Returns the number of enumerations that iteration of the Dictionary
GetType Get type of the current instance (inherited from Object)
Remove Remove the value of the specified key from Dictionary
Tostring Returns String (inherited from Object) that represents the current Object
TryGetValue Gets the value associated with the specified key