How to Add Elements to a C# Dictionary?
C# Dictionary class constructor takes a key data type and a value data type. Both types are generic, so they can be any .NET data type.
· The following Dictionary class is a generic class and can store any data type.
Dictionary<string, string> EmployeeList = new Dictionary<string, string>();
EmployeeList.Add(“Mahesh Chand”, “Programmer”);
EmployeeList.Add(“Praveen Kumar”, “Project Manager”);
EmployeeList.Add(“Raj Kumar”, “Architect”);
EmployeeList.Add(“Nipun Tomar”, “Asst. Project Manager”);
EmployeeList.Add(“Dinesh Beniwal”, “Manager”);
· The following code below creates a dictionary where the key type is a string, and the value type is a short integer.
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add(“Mahesh Chand”, 35);
AuthorList.Add(“Mike Gold”, 25);
AuthorList.Add(“Praveen Kumar”, 29);
AuthorList.Add(“Raj Beniwal”, 21);
AuthorList.Add(“Dinesh Beniwal”, 84);
· We can also limit the size of a dictionary. The following code below creates a dictionary where the key type is a string, the value type is float, and the total number of items it can hold is 3.
Dictionary<string, float> PriceList = new Dictionary<string, float>(3);
PriceList.Add(“Tea”, 3.25f);
PriceList.Add(“Juice”, 2.76f);
PriceList.Add(“Milk”, 1.15f);
How to Retrieve Elements from a C# Dictionary?
· The Dictionary is a collection. So, we can use the foreach loop to go through all the items and read them using the Key ad Value properties.
foreach (KeyValuePair<string, Int16> author in AuthorList)
{
Console.WriteLine(“Key: {0}, Value: {1}”, author.Key, author.Value);
}
public void CreateDictionary()
{
// Create a dictionary with string key and Int16 value pair
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add(“Mahesh Chand”, 35);
AuthorList.Add(“Mike Gold”, 25);
AuthorList.Add(“Praveen Kumar”, 29);
AuthorList.Add(“Raj Beniwal”, 21);
AuthorList.Add(“Dinesh Beniwal”, 84);
// Read all data
Console.WriteLine(“Authors List”);
foreach (KeyValuePair<string, Int16> author in AuthorList)
{
Console.WriteLine(“Key: {0}, Value: {1}”, author.Key, author.Value);
}
}
· The Dictionary class has three properties – Count, Keys, and Values.
How do you get the number of elements in a C# dictionary?
· The Count property gets the number of key/value pairs in a dictionary.
Console.WriteLine(“Count: {0}”, AuthorList.Count);
How to get an item from a C# dictionary?
· The Item property gets and sets the value associated with the specified key.
// Set Item value
AuthorList[“Mahesh Chand”] = 20;
// Get Item value
Int16 age = Convert.ToInt16(AuthorList[“Mahesh Chand”]);
· How do you get a collection of keys in a C# dictionary?
· he Keys property gets a collection containing the keys in the Dictionary. It returns an object of KeyCollection type
// Get and display keys
Dictionary<string, Int16>.KeyCollection keys = AuthorList.Keys;
foreach (string key in keys)
{
Console.WriteLine(“Key: {0}”, key);
}
How to get the collection of values of a C# dictionary?
· The Values property gets a collection containing the values in the Dictionary. It returns an object of ValueCollection type.
// Get and display values
Dictionary<string, Int16>.ValueCollection values = AuthorList.Values;
foreach (Int16 val in values)
{
Console.WriteLine(“Value: {0}”, val);
}
Leave a Reply
Want to join the discussion?Feel free to contribute!