Please read the following article first before we start this
https://adriancs.com/c-sharp/491/c-tutorial-list/
Go ahead, I’ll wait. It’s contains some basic info about C#. This article will assume that you have already knew some of the basics of C#.
Finished reading? Cool, let’s continue.
C# Dictionary
contains a pair of Key
and Value
. It looks like this:
Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
Both TKey
and TValue
can be any object Type.:
- string object
- int object
- decimal object
- DateTime object
- any class object
- etc…
For example:
Dictionary<string, decimal> dic1 = new Dictionary<string, decimal>();
Dictionary<double, DateTime> dic2 = new Dictionary<double, DateTime>();
Dictionary<string, string> dic3 = new Dictionary<string, string>();
It can stores Class Object Type
Example 1: Type of Class Object “Book
“
class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}
Dictionary<string, Book> dic = new Dictionary<string, Book>();
Example 2: Type of Class Object “Member
“
class Member
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Code { get; set; }
}
Dictionary<int, Member> dic = new Dictionary<int, Member>();
So at this point, you’ve learnt that the object Type of Dictionary
<TKey, TValue>
, both “Key” and “Value” can be any Type.
To learn about the basic functionality of Dictionary, let’s take the following object Type as example:
Dictionary<int, string> dic = new Dictionary<int, string>();
Let’s start. The basic functionality is explained in code comment
// ===========================
// Declare new dictionary
// ===========================
Dictionary<int, string> dic = new Dictionary<int, string>();
// assign or add new values into dictionary
dic[4] = "apple";
dic[6] = "orange";
dic[11] = "apple";
dic[3] = "pineapple";
dic[1] = "stawberry";
// ===========================
// Getting single value out of dictionary
// ===========================
string output = dic[1];
// stawberry
output = dic[2];
// error. The key "2" is not existed
output = dic[5];
// error. The key "5" is not existed
// assign key of "5" and it's value into dictionary
dic[5] = "banana";
output = dic[5];
// banana
// success getting the value now,
// because "5" is added as key into thel dictionary
// ===========================
// Adding new keys/values into dictionary
// ===========================
dic.Add(21, "banana"); // success
dic.Add(23, "banana"); // success
dic.Add(25, "banana"); // success
dic.Add(29, "banana"); // success
dic.Add(29, "blueberry"); // error
// this method can only add keys that are not existed
// 29 already added
dic[29] = "blueberry";
// success
// this method is "assign or add"
// if the key existed, update the value
// if the key is not added, add it
// ===========================
// Changing data/value
// ===========================
dic[21] = "lemon";
// key 21 is changed to lemon
dic[21] = "avocados";
// key 21 is now changed to avocados
dic[21] = "pears";
// key 21 is now changed to pears
// ===========================
// Remove keys from dictionary
// ===========================
dic.Remove(1); // success
dic.Remove(1); // key 1 is removed, but will not cause error
dic.Remove(2); // key 2 is not existed, nothing happens
dic.Remove(3); // success
dic.Remove(4); // success
dic.Remove(5); // success
dic.Remove(6); // key 6 is not existed, nothing happens
// ===========================
// Check if the key existed
// ===========================
if (dic.ContainsKey(100))
{
// yes - key existed
}
else
{
// no = key is not existed
}
Getting all keys and values of dictionary in Foreach Loop:
foreach (KeyValuePair<int, string> kv in dic)
{
}
Or even simpler, use the keyword “var
” in Foreach Loop:
foreach (var kv in dic)
{
}
They both work the same.
Let’s start:
StringBuilder sb = new StringBuilder();
foreach (var kv in dic)
{
sb.Append($"Key: {kv.Key}, Value: {kv.Value} <br />");
}
ph1.Controls.Add(new LiteralControl(sb.ToString()));
Using Class Object Type as value in Dictionary
Take this class as example:
class Member
{
public int Id { get; set; }
public string Name { get; set; }
public string Tel { get; set; }
}
Declare the dictionary:
Member a = new Member();
a.Id = 3;
a.Name = "John";
a.Tel = "28394792";
Member b = new Member();
b.Id = 5;
b.Name = "Irene";
b.Tel = "2397642";
Member c = new Member();
c.Id = 6;
c.Name = "Smith";
c.Tel = "23984732";
Dictionary<int, Member> dic = new Dictionary<int, Member>();
dic[3] = a;
dic[5] = b;
dic[6] = c;
StringBuilder sb = new StringBuilder();
sb.Append(@"
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Tel</th>
</tr>
");
foreach(var kv in dic)
{
int id = kv.Key;
var m = kv.Value;
sb.Append($@"
<tr>
<td>{id}</td>
<td>{m.Name}</td>
<td>{m.Tel}</td>
</tr>
");
}
sb.Append("</table>");
ph1.Controls.Add(new LiteralControl(sb.ToString()));
Changing value by accesing the key
Member c = new Member();
c.Id = 6;
c.Name = "Smith";
c.Tel = "23984732";
Dictionary<int, Member> dic = new Dictionary<int, Member>();
dic[6] = c;
string output = dic[6].Name;
// Smith
dic[6].Name = "Henry";
output = dic[6].Name;
// Henry
// Note that the name of "Smith" is changed to "Henry"