Csharp/CSharp Tutorial/Data Structure/IDictionary

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Creating a list from a dictionary

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>();
       List<KeyValuePair<string, DateTime>> keyValueList = new List<KeyValuePair<string, DateTime>>(dictionary);
   }

}</source>

IDictionary<TKey,TValue>: Add some entries into the dictionary

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
   }

}</source>

IDictionary<TKey,TValue>: Check whether certain keys exist in the map

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
       
       Console.WriteLine(salaryMap.ContainsKey("S")); 
       Console.WriteLine(salaryMap.ContainsKey("T"));
   }

}</source>

True
False

IDictionary<TKey,TValue>: iterate over the map and add up the values

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
       decimal total = 0.0M;
       foreach (decimal d in salaryMap.Values)
           total += d;
       Console.WriteLine("{0:C}", total);
       
   }

}</source>

$101.49

IDictionary<TKey,TValue>: Iterating over map/value pairs

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
       foreach (KeyValuePair<string, decimal> kvp in salaryMap)
           Console.WriteLine("{0} == {1}", kvp.Key, kvp.Value);
   }

}</source>

S == 60.5
W == 10.0
J == 30.99

IDictionary<TKey,TValue>: remove one entry

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
       
       salaryMap.Remove("W");
   }

}</source>

IDictionary<TKey,TValue>: Retrieve some values from the map

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {

   public static void Main()
   {
       IDictionary<string, decimal> salaryMap = new Dictionary<string, decimal>();
       salaryMap.Add("S", 60.5M);
       salaryMap.Add("W", 10.0M);
       salaryMap.Add("J", 30.99M);
       
       Console.WriteLine("{0:C}", salaryMap["S"]); 
       Console.WriteLine("{0:C}", salaryMap["J"]);
   }

}</source>

$60.50
$30.99