Csharp/CSharp Tutorial/Data Structure/IDictionary — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:15, 26 мая 2010
Содержание
- 1 Creating a list from a dictionary
- 2 IDictionary<TKey,TValue>: Add some entries into the dictionary
- 3 IDictionary<TKey,TValue>: Check whether certain keys exist in the map
- 4 IDictionary<TKey,TValue>: iterate over the map and add up the values
- 5 IDictionary<TKey,TValue>: Iterating over map/value pairs
- 6 IDictionary<TKey,TValue>: remove one entry
- 7 IDictionary<TKey,TValue>: Retrieve some values from the map
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