A dictionary in C# is a data structure that consists of key-value pairs, enabling efficient retrieval of values based on specific keys. The Dictionary<TKey, TValue> class in C# is an integral component of the .NET Framework and is commonly employed for constructing dictionaries.
The SortedDictionary<TKey, TValue> represents a sorted collection of key-value pairs. Accessing the Keys property of SortedDictionary<TKey, TValue> provides a structured set that represents the keys within the dictionary.
Syntax:
It has the following Syntax:
public System.Collections.Generic.SortedDictionary<TKeys, TValues>.KeyCollection Keys { get; }
Return Value:
It reruns the collection of keys.
Key characteristics include:
- Arranged Sequence: The Keys property provides the keys in the dictionary in the sequence they were added, or following the specified custom comparer during the creation of the SortedDictionary.
- Immutable Key Collection: The KeyCollection generated by Keys is immutable, preventing direct alteration of its elements.
- Access Complexity: The Keys property provides a direct reference to the organized collection of keys. Hence, it represents an O(1) oepration.
- Iterating difficulty: It iterating through a KeyCollection has an O(n) difficulty because it requires traversal of all keys.
- Key Retrieval: When you only require the keys from the SortedDictionary , the Keys property makes it simple to find them.
- It is handy for iterating over keys while doing operations on the dictionary's related values.
- Immutable Collection: Changes made to the original SortedDictionary have no impact on the KeyCollection because it is an immutable collection.
- Changes Should Be Avoided: Because the KeyCollection is meant to be read-only, altering the keys within it may result in exceptions.
Complexity and performance:
Considerations as well as Use Cases:
Best Practices and Considerations:
Altering the KeyCollection (adding or deleting keys) will lead to a NotSupportedException being thrown.
Example 1:
Let's consider an example to demonstrate how to implement the dictionary functionality in C#.
using System;
using System.Collections.Generic;
class Dic{
public static void Main()
{
// Create a new dictionary which consists of string keys
SortedDictionary<string, string> myD =
new SortedDictionary<string, string>();
//The key and value pairs are added to the dictionary
myD.Add("Oman", "Muscat");
myD.Add("Belgium", "Brussels");
myD.Add("Germany", "Berlin");
myD.Add("China", "Beijing");
myD.Add("France", "Paris");
myD.Add("Spain", "Madrid");
// to find the count of the keys
Console.WriteLine("The total number of key/value pairs"
+ " in myD are : " + myD.Count);
//For getting only the keys, make use of the Keys property.
SortedDictionary<string, string>.KeyCollection keyColls =
myD.Keys;
//The KeyCollection components are strongly typed using the type // that was defined for dictionary // keys.
foreach(string str in keyColls)
{
Console.WriteLine("Key = {0}", str);
}
}
}
Output:
The total number of key/value pairs in myD are : 6
Key = Belgium
Key = China
Key = France
Key = Germany
Key = Oman
Key = Spain
Example 2:
Let's consider another example to incorporate the dictionary feature in C#.
using System;
using System.Collections.Generic;
class Dic{
public static void Main()
{
// Create a new dictionary which consists of string keys
SortedDictionary<int, int> myD =
new SortedDictionary<int, int>();
//The key and value pairs are added to the dictionary
myD.Add(1,5);
myD.Add(2,6);
myD.Add(3,7);
myD.Add(4,8);
myD.Add(5,9);
myD.Add(0,54);
// to find the count of the keys
Console.WriteLine("The total number of key/value pairs"
+ " in myD are : " + myD.Count);
//For getting only the keys, make use of the Keys property.
SortedDictionary<int, int>.KeyCollection keyColls =
myD.Keys;
//The KeyCollection components are strongly typed using the type // that was defined for dictionary // keys.
foreach(int i in keyColls)
{
Console.WriteLine("Key = {0}", i);
}
}
}
Output:
The total number of key/value pairs in myD are : 6
Key = 0
Key = 1
Key = 2
Key = 3
Key = 4
Key = 5