Implementation of C# Dictionary class: Add() method
Program.cs
using System.Collections.Generic;

Dictionary<long, string> dict = new Dictionary<long, string>();
dict.Add(25622348989, "James Moore");
dict.Add(25622348990, "Donald Taylor");
dict.Add(25622348991, "Edward Parkar");
dict.Add(25622348992, "Ryan Bakshi");

foreach (KeyValuePair<long, string> kv in dict)
{
    Console.WriteLine("Account No: {0}, Name: {1}", kv.Key, kv.Value);
}
Console.WriteLine("Size: " + dict.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Account No: 25622348989, Name: James Moore
Account No: 25622348990, Name: Donald Taylor
Account No: 25622348991, Name: Edward Parkar
Account No: 25622348992, Name: Ryan Bakshi
Size: 4
kodingwindow@kw:~/csharp$ 
Implementation of C# Dictionary class: ContainsKey() and ContainsValue() methods
Program.cs
using System.Collections.Generic;

Dictionary<long, string> dict = new Dictionary<long, string>();
dict.Add(25622348989, "James Moore");
dict.Add(25622348990, "Donald Taylor");
dict.Add(25622348991, "Edward Parkar");
dict.Add(25622348992, "Ryan Bakshi");

Console.WriteLine(dict.ContainsKey(25622348991));
Console.WriteLine(dict.ContainsValue("Edward Taylor"));
Output
kodingwindow@kw:~/csharp$ dotnet run
True
False
kodingwindow@kw:~/csharp$ 
Implementation of C# Dictionary class: Remove() and Clear() methods
Program.cs
using System.Collections.Generic;

Dictionary<long, string> dict = new Dictionary<long, string>();
dict.Add(25622348989, "James Moore");
dict.Add(25622348990, "Donald Taylor");
dict.Add(25622348991, "Edward Parkar");
dict.Add(25622348992, "Ryan Bakshi");

dict.Remove(25622348991);
foreach (KeyValuePair<long, string> kv in dict)
{
    Console.WriteLine("Account No: {0}, Name: {1}", kv.Key, kv.Value);
}

dict.Clear();
if (dict.Count == 0)
{
    Console.WriteLine("Dictionary is empty now");
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Account No: 25622348989, Name: James Moore
Account No: 25622348990, Name: Donald Taylor
Account No: 25622348992, Name: Ryan Bakshi
Dictionary is empty now
kodingwindow@kw:~/csharp$ 
Advertisement