Imagine a phonebook.
It's a list of people's names (keys) and their corresponding phone numbers (values). The special thing about a phonebook is that it's always sorted alphabetically by the names. This makes it incredibly easy to find someone's number.
A SortedList is like a digital phonebook. It stores pairs of information (keys and values) and keeps them organized alphabetically or numerically, depending on the type of key. This makes it efficient for searching and retrieving specific data.
Why use a SortedList?
Efficient Retrieval: Quickly find elements by their keys.
Automatic Sorting: Keys are always sorted, making searching and iteration easier.
Flexible Data Storage: Can store diverse data types as keys and values.
using System.Collections;
// Create a new SortedList
SortedList sortedList = new SortedList();
// Add key-value pairs
sortedList.Add("Apple", 10); // Add "Apple" with a value of 10
sortedList.Add("Banana", 20); // Add "Banana" with a value of 20
sortedList.Add("Cherry", 15); // Add "Cherry" with a value of 15
// Accessing by key
int appleCount = (int)sortedList["Apple"]; // Get the value associated with "Apple"
Console.WriteLine("Apple count: " + appleCount);
// Accessing by index
object secondItem = sortedList.GetByIndex(1); // Get the second item in the list
Console.WriteLine("Second item: " + secondItem);
// Iterate using foreach loop
foreach (DictionaryEntry entry in sortedList)
{
Console.WriteLine("Key: " + entry.Key + ", Value: " + entry.Value); // Print each key-value pair
}
// Remove by key
sortedList.Remove("Banana"); // Remove the entry with the key "Banana"
// Remove by index
sortedList.RemoveAt(0); // Remove the first item in the list
// Check if a key exists
bool containsApple = sortedList.ContainsKey("Apple"); // Check if "Apple" is a key
Console.WriteLine("Contains Apple: " + containsApple);
// Get all keys
ICollection keys = sortedList.Keys; // Get a collection of all keys
// Get all values
ICollection values = sortedList.Values; // Get a collection of all values
// Example: Sorting a List of Names and Ages
SortedList nameAgeList = new SortedList();
nameAgeList.Add("Alice", 25); // Add "Alice" with an age of 25
nameAgeList.Add("Bob", 30); // Add "Bob" with an age of 30
nameAgeList.Add("Charlie", 22); // Add "Charlie" with an age of 22
// Sort by name (ascending order)
foreach (DictionaryEntry entry in nameAgeList)
{
Console.WriteLine("Name: " + entry.Key + ", Age: " + entry.Value); // Print each name-age pair
}
Important Considerations:
Key Uniqueness: Each key in a
SortedList
must be unique.Key Type: Keys must be comparable, often using the
IComparable
interface.Null Values: Keys cannot be null, but values can be.
Performance: Consider the performance implications of frequent additions and removals, as it might involve reordering the internal structure.
Remember: While non-generic SortedList
is still usable, it's generally recommended to use the generic SortedList<TKey, TValue>
for type safety and better performance in modern C# applications.
#CSharp #SortedList #Programming #CodeExplanation #Tutorial #LearnToCode #DeveloperTips #CodingTutorial #CSharpTutorial