Mid Collections

How would you iterate through a SortedList<TKey, TValue>?

You can use a foreach loop over KeyValuePair<TKey, TValue> elements, which

iterates in sorted key order:

foreach (var kvp in sortedList)

Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");

You can also iterate over keys or values separately:

foreach (var key in sortedList.Keys) { /* ... */ }

foreach (var value in sortedList.Values) { /* ... */ }

📘 C# SortedSet<T> – Interview

Questions & Answers

More from C# Programming Tutorial

All questions for this course