Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dictionary<int, string> dic;
- foreach(string s in dic.Values)
- {
- Console.WriteLine(s);
- }
- foreach(KeyValuePair<int, string> kvp in dic)
- {
- Console.WriteLine("Key : " + kvp.Key.ToString() + ", Value : " + kvp.Value);
- }
- foreach(int key in dic.Keys)
- {
- Console.WriteLine(key.ToString());
- }
- Dictionary<int, string> newValues = new Dictionary<int, string>() { 1, "Test" };
- foreach(KeyValuePair<int, string> kvp in newValues)
- {
- dic[kvp.Key] = kvp.Value; // will automatically add the item if it's not there
- }
- List<int> keys = new List<int>() { 1, 3 };
- foreach(int key in keys)
- {
- dic.Remove(key);
- }
- Dictionary<int,int> dic=new Dictionary<int, int>();
- //...fill the dictionary
- int[] keys = dic.Keys.ToArray();
- foreach (int i in keys)
- {
- dic.Remove(i);
- }
- public static void MutateEach(this IDictionary<TKey, TValue> dict, Func<TKey, TValue, KeyValuePair<TKey, TValue>> mutator)
- {
- var removals = new List<TKey>();
- var additions = new List<KeyValuePair<TKey, TValue>>();
- foreach (var pair in dict)
- {
- var newPair = mutator(pair.Key, pair.Value);
- if ((newPair.Key != pair.Key) || (newPair.Value != pair.Value))
- {
- removals.Add(pair.Key);
- additions.Add(newPair);
- }
- }
- foreach (var removal in removals)
- dict.Remove(removal);
- foreach (var addition in additions)
- dict.Add(addition.Key, addition.Value);
- }
- myDict.MutateEach(key => key.ToLower(), value => value.Trim());
Add Comment
Please, Sign In to add comment