The dictionaries in C# are quite interesting feature. For a person, coming from the VBA world ( 🙂 ), this feature is quite interesting and new. A lot has been said in the internet about dictionaries, so I will not try to say it again. With two words, dictionaries are systems of lists, with a key and a value. The key can be of any type, the value as well. You should be careful that the keys are always unique and… that is it, lets go for the example.
In our example, we will build a dictionary named PlayerReview, consisted of integer for a key and object from class “Footballer” for value. The object from the class “Footballer” has two properties – Age and Club. The property Club is enumerated to 4 possibilities – CSKA, Levski, Slavia and Lokomotiv. The code for the class and the enum look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Footballer { public int Age; public FootballClub Club; } public enum FootballClub { CSKA, Levski, Slavia, Lokomotiv, } |
The interesting part starts when we fill up our dictionary:
1 2 3 4 5 6 7 |
Dictionary<int, Footballer> PlayerReview = new Dictionary<int, Footballer>(); PlayerReview.Add(1, new Footballer() {Age = 20, Club = FootballClub.CSKA }); PlayerReview.Add(2, new Footballer() {Age = 25, Club = FootballClub.CSKA }); PlayerReview.Add(3, new Footballer() {Age = 32, Club = FootballClub.Slavia }); PlayerReview.Add(4, new Footballer() {Age = 24, Club = FootballClub.Lokomotiv }); PlayerReview.Add(5, new Footballer() {Age = 23, Club = FootballClub.Levski }); |
At the first row, we initialize the Dictionary called Player Review. At the rows later, we fill it up with 5 players, giving them ID (the keys), Age (20 to 32) and Clubs (from the enum possibilities).
Once we have the Dictionary, the question is what can we do with it? With 3 foreach loops, I show how to display data for the Keys, Age and the Values, separately and united. The code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int totalPlayers = PlayerReview.Count; Console.WriteLine("We have {0} players:",totalPlayers); foreach (KeyValuePair <int,Footballer> myList in PlayerReview) Console.WriteLine("Key - Value : {0} : {1}, {2}", myList.Key,myList.Value.Age,myList.Value.Club); foreach (Footballer myPlayer in PlayerReview.Values) { Console.WriteLine("Age: {0}", myPlayer.Age); } foreach (int myKey in PlayerReview.Keys) { Console.WriteLine("ID: {0}", myKey); } |
The result at the console is the following:
Pretty much, that is all for the current example. Creating a Dictionary with object and integer is not that difficult in C#. Here comes the whole code, if you need it for any reason:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class FootBallPlayers { static void Main() { Dictionary<int, Footballer> PlayerReview = new Dictionary<int, Footballer>(); PlayerReview.Add(1, new Footballer() {Age = 20, Club = FootballClub.CSKA }); PlayerReview.Add(2, new Footballer() {Age = 25, Club = FootballClub.CSKA }); PlayerReview.Add(3, new Footballer() {Age = 32, Club = FootballClub.Slavia }); PlayerReview.Add(4, new Footballer() {Age = 24, Club = FootballClub.Lokomotiv }); PlayerReview.Add(5, new Footballer() {Age = 23, Club = FootballClub.Levski }); int totalPlayers = PlayerReview.Count; Console.WriteLine("We have {0} players:",totalPlayers); foreach (KeyValuePair <int,Footballer> myList in PlayerReview) Console.WriteLine("Key - Value : {0} : {1}, {2}", myList.Key,myList.Value.Age,myList.Value.Club); foreach (Footballer myPlayer in PlayerReview.Values) { Console.WriteLine("Age: {0}", myPlayer.Age); } foreach (int myKey in PlayerReview.Keys) { Console.WriteLine("ID: {0}", myKey); } } } public class Footballer { public int Age; public FootballClub Club; } public enum FootballClub { CSKA, Levski, Slavia, Lokomotiv, } |
Have a nice day! 😀