Sorteddictionary.Keys Property In C#

A dictionary is a collection type in C# that includes key-value pairs, allowing for quick access to things by unique keys. The Dictionary<TKey, TValue> class in C# is part of the .NET Framework and is frequently used to build dictionaries.

The SortedDictionary<TKey, TValue> is a collection containing sorted key-value pairs. The Keys property of SortedDictionary<TKey, TValue> gives access to a structured set representing the dictionary's keys.

Syntax:

It has the following Syntax:

Example

public System.Collections.Generic.SortedDictionary<TKeys, TValues>.KeyCollection Keys { get; }

Return Value:

It reruns the collection of keys.

Key characteristics include:

There are several characteristics of the SortedDictionary.Keys Property in C#. Some characteristics of this property are as follows:

  • Ordered Collection: The Keys property returns the keys in the dictionary in the order they have been created, or by using the custom comparer provided when the SortedDictionary was formed.
  • KeyCollection Is Immutable: The KeyCollection created by Keys is immutable, which means that its elements cannot be directly modified.
  • Complexity and performance:

  • 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.
  • Considerations as well as Use Cases:

  • 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.
  • Best Practices and Considerations:

  • 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.
  • Limitations and Exemptions:

  • Modifying the KeyCollection (adding or removing keys) results in NotSupportedException .
  • Example 1:

Let us take an example to implement the dictionary function in C#.

Example

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:

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 us take another example to implement the dictionary function in C#.

Example

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:

Output

The total number of key/value pairs in myD are : 6
Key = 0
Key = 1
Key = 2
Key = 3
Key = 4
Key = 5

Input Required

This code uses input(). Please provide values below: