C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We loop over the List instance, using a foreach-loop with the KeyValuePair iteration variable.
KeyValuePairFinally: We print all the Key and Value properties with the Console.WriteLine method.
ConsoleC# program that calls ToList on Dictionary
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary["cat"] = 1;
dictionary["dog"] = 4;
dictionary["mouse"] = 2;
dictionary["rabbit"] = -1;
// Call ToList.
List<KeyValuePair<string, int>> list = dictionary.ToList();
// Loop over list.
foreach (KeyValuePair<string, int> pair in list)
{
Console.WriteLine(pair.Key);
Console.WriteLine(" {0}", pair.Value);
}
}
}
Output
cat
1
dog
4
mouse
2
rabbit
-1
ContainsKey: This method avoids the chance of an exception being raised. If we access a key directly, we might get an exception.
ContainsKeyPart 1: We add 4 string values. The List then has 4 elements. We need to add these 4 elements to the Dictionary next.
Part 2: We create a Dictionary with string keys. We loop through the List, calling Add if ContainsKey returns false.
Part 3: We display the contents of the Dictionary we created and populated. We see that the strings from the List are the new keys.
C# program that converts List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Part 1: create new List.
List<string> list = new List<string>();
list.Add("Olympics");
list.Add("Nascar");
list.Add("Super Bowl");
list.Add("Wimbledon");
// Part 2: put List values into Dictionary.
var exampleDictionary = new Dictionary<string, int>();
foreach (string value in list)
{
if (!exampleDictionary.ContainsKey(value))
{
exampleDictionary.Add(value, 1);
}
}
// Part 3: display Dictionary.
foreach (var pair in exampleDictionary)
{
Console.WriteLine(pair);
}
}
}
Output
[Olympics, 1]
[Nascar, 1]
[Super Bowl, 1]
[Wimbledon, 1]
Part 1: Here we fill the Dictionary by calling the Add method with 2 arguments. We add the names of pet birds.
Part 2: We use the List constructor on the Keys from the Dictionary. It accepts an IEnumerable collection with an element type of string.
ListPart 3: This part is the same as the previous part except it uses the List constructor with a parameter of the Values collection.
C# program that gets Keys and Values
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Part 1: example Dictionary.
Dictionary<string, int> birdDictionary = new Dictionary<string, int>();
birdDictionary.Add("parakeet", 3);
birdDictionary.Add("parrot", 5);
birdDictionary.Add("finch", 7);
birdDictionary.Add("lovebird", 9);
// Part 2: get List of keys.
List<string> keyList = new List<string>(birdDictionary.Keys);
// Display them.
foreach (var value in keyList)
{
Console.WriteLine(value);
}
// Part 3: get List of values.
List<int> valueList = new List<int>(birdDictionary.Values);
// Display them.
foreach (var value in valueList)
{
Console.WriteLine(value);
}
}
}
Output
parakeet
parrot
finch
lovebird
3
5
7
9
Thus: The Dictionary's implementation allows the ToList extension method to work here.
IEnumerableList: It is faster to loop over and requires less memory due to its lack of an internal buckets structure.
Dictionary vs. List