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
SortedDictionary.Item[] Property in C#
The SortedDictionary.Item[] property in C# provides indexer access to get or set values associated with specified keys in a sorted dictionary. This property allows you to access dictionary elements using square bracket notation, making it convenient to retrieve and modify values.
Syntax
Following is the syntax for the SortedDictionary.Item[] property −
public TValue this[TKey key] { get; set; }
Parameters
-
key − The key of the value to get or set of type
TKey.
Return Value
Returns the value of type TValue associated with the specified key. If the key is not found during a get operation, a KeyNotFoundException is thrown.
Using Item[] Property for Getting Values
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(1, "One");
sortedDict.Add(2, "Two");
sortedDict.Add(3, "Three");
sortedDict.Add(4, "Four");
sortedDict.Add(5, "Five");
sortedDict.Add(6, "Six");
Console.WriteLine("SortedDictionary key-value pairs...");
foreach (var pair in sortedDict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
Console.WriteLine("Count of SortedDictionary key-value pairs = " + sortedDict.Count);
Console.WriteLine("Value at key 5 = " + sortedDict[5]);
Console.WriteLine("Value at key 2 = " + sortedDict[2]);
}
}
The output of the above code is −
SortedDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Count of SortedDictionary key-value pairs = 6 Value at key 5 = Five Value at key 2 = Two
Using Item[] Property for Setting Values
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(1, "SUV");
sortedDict.Add(2, "MUV");
sortedDict.Add(3, "Utility Vehicle");
sortedDict.Add(4, "AUV");
sortedDict.Add(5, "Hatchback");
Console.WriteLine("Original value at key 5: " + sortedDict[5]);
// Update existing value
sortedDict[5] = "Crossover";
Console.WriteLine("Updated value at key 5: " + sortedDict[5]);
// Add new key-value pair using indexer
sortedDict[6] = "Convertible";
Console.WriteLine("New value at key 6: " + sortedDict[6]);
Console.WriteLine("\nAll key-value pairs:");
foreach (var pair in sortedDict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
}
}
The output of the above code is −
Original value at key 5: Hatchback Updated value at key 5: Crossover New value at key 6: Convertible All key-value pairs: Key = 1, Value = SUV Key = 2, Value = MUV Key = 3, Value = Utility Vehicle Key = 4, Value = AUV Key = 5, Value = Crossover Key = 6, Value = Convertible
Key Behavior
-
When using the getter (
sortedDict[key]), if the key doesn't exist, aKeyNotFoundExceptionis thrown. -
When using the setter (
sortedDict[key] = value), if the key exists, its value is updated. If the key doesn't exist, a new key-value pair is added. -
Keys in
SortedDictionaryare automatically maintained in sorted order.
Conclusion
The SortedDictionary.Item[] property provides convenient indexer access for getting and setting values by key. It automatically handles both value updates for existing keys and insertion of new key-value pairs, while maintaining the sorted order of keys in the dictionary.
