Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get or set the value associated with specified key in ListDictionary in C#
The ListDictionary class in C# provides an indexer that allows you to get or set values using a specified key. The indexer dict[key] provides convenient access to elements in the dictionary by their key.
ListDictionary is part of the System.Collections.Specialized namespace and is optimized for small collections where the number of elements is usually 10 or fewer.
Syntax
Following is the syntax for getting a value using the indexer −
object value = listDictionary[key];
Following is the syntax for setting a value using the indexer −
listDictionary[key] = value;
Parameters
key − The key whose associated value to get or set
value − The value to associate with the specified key
Return Value
When getting a value, the indexer returns the object associated with the specified key. If the key is not found, it returns null.
Getting Values from ListDictionary
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Appliances");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("Value associated with key C = " + dict["C"]);
Console.WriteLine("Value associated with key A = " + dict["A"]);
Console.WriteLine("Value associated with key F = " + dict["F"]);
}
}
The output of the above code is −
Value associated with key C = Appliances Value associated with key A = Books Value associated with key F = Footwear
Setting Values in ListDictionary
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Appliances");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("Original value for key C = " + dict["C"]);
dict["C"] = "Hardware";
Console.WriteLine("Updated value for key C = " + dict["C"]);
// Setting value for a new key
dict["G"] = "Sports";
Console.WriteLine("Value for new key G = " + dict["G"]);
}
}
The output of the above code is −
Original value for key C = Appliances Updated value for key C = Hardware Value for new key G = Sports
Handling Non-Existent Keys
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
// Accessing a non-existent key
object value = dict["X"];
if (value == null) {
Console.WriteLine("Key 'X' does not exist in the dictionary");
}
// Setting value for non-existent key creates new entry
dict["X"] = "New Item";
Console.WriteLine("Value for key X = " + dict["X"]);
}
}
The output of the above code is −
Key 'X' does not exist in the dictionary Value for key X = New Item
Conclusion
The ListDictionary indexer dict[key] provides a convenient way to access and modify values by key. When getting a value, it returns null for non-existent keys, and when setting a value, it either updates existing entries or creates new ones.
