C# | SortedDictionary.Keys Property Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs to illustrate the use of the above-discussed property: Example 1: csharp // C# code to get the keys // in the SortedDictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new SortedDictionary // of strings, with string keys. SortedDictionary<string, string> myDict = new SortedDictionary<string, string>(); // Adding key/value pairs in myDict myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs" + " in myDict are : " + myDict.Count); // To get the keys alone, // use the Keys property. SortedDictionary<string, string>.KeyCollection keyColl = myDict.Keys; // The elements of the KeyCollection // are strongly typed with the type // that was specified for dictionary // keys foreach(string s in keyColl) { Console.WriteLine("Key = {0}", s); } } } Output: Total key/value pairs in myDict are : 6 Key = Australia Key = Belgium Key = China Key = India Key = Netherlands Key = Russia Example 2: csharp // C# code to get the keys // in the SortedDictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new SortedDictionary // of ints, with int keys. SortedDictionary<int, int> myDict = new SortedDictionary<int, int>(); // Adding key/value pairs in myDict myDict.Add(9, 8); myDict.Add(3, 4); myDict.Add(4, 7); myDict.Add(1, 7); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs" + " in myDict are : " + myDict.Count); // To get the keys alone, // use the Keys property. SortedDictionary<int, int>.KeyCollection keyColl = myDict.Keys; // The elements of the KeyCollection // are strongly typed with the type // that was specified for dictionary // keys foreach(int s in keyColl) { Console.WriteLine("Key = {0}", s); } } } Output: Total key/value pairs in myDict are : 4 Key = 1 Key = 3 Key = 4 Key = 9 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sorteddictionary-2.keys?view=netframework-4.7.2 Comment R rupesh_rao Follow 0 Improve R rupesh_rao Follow 0 Improve Article Tags : C# CSharp-Generic-Namespace CSharp SortedDictionary Class Explore IntroductionC# Tutorial 2 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read Encapsulation in C# 2 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read Method Parameters in C# 4 min read Method Overriding in C# 7 min read Anonymous Method in C# 2 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read ArrayList Class in C# 4 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 5 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like