Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Creating a list from a dictionary
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);
}
}
IDictionary<TKey,TValue>: Add some entries into the dictionary
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);
}
}
IDictionary<TKey,TValue>: Check whether certain keys exist in the map
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"));
}
}
True
False
IDictionary<TKey,TValue>: iterate over the map and add up the values
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);
}
}
$101.49
IDictionary<TKey,TValue>: Iterating over map/value pairs
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);
}
}
S == 60.5
W == 10.0
J == 30.99
IDictionary<TKey,TValue>: remove one entry
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");
}
}
IDictionary<TKey,TValue>: Retrieve some values from the map
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"]);
}
}
$60.50
$30.99