ูู ุงูู
ูุงู ุฏูุ ููุชููู
ุนู SortedListุ ููู collection ูุฑูุฏ ู
ู ููุนู ุจูุฌู
ุน ุจูู ู
ู
ูุฒุงุช ุงูู Array ูุงูู Hashtable. ูู ู
ูุฌูุฏ ูู System.Collections ูุจูุนุชุจุฑ Non-Generic.
What is SortedList?
ุงูู SortedList ูู collection ู
ู ููุน Key/Value pairsุ ูุงูู
ูุฒุฉ ุงูุฃุณุงุณูุฉ ุจุชุงุนุชู ุฅู ุนูุงุตุฑู ุฏุงูู
ูุง ุจุชููู ู
ุชุฑุชุจุฉ ุชุตุงุนุฏููุง ุนูู ุญุณุจ ุงูู Keys.
ุฏู ู ุนูุงู ุฅูู ุจูููุฑูู ุทุฑููุชูู ูููุตูู ููุนูุงุตุฑ:
- ุนู ุทุฑูู ุงูู Key (ุฒู ุงูู
Hashtable). - ุนู ุทุฑูู ุงูู Index (ุฒู ุงูู
ArrayList).
Properties of SortedList
- ุงูู Interfaces: ุจูููุฐ ุงูู
interfacesุงููู ุงุณู ูุงIEnumerable,ICollection,IDictionary, ูICloneable. - ุงูู Internal Structure: ู
ู ุฌููุ ูู ุจูุญุชูุธ ุจู
two arrays: ูุงุญุฏ ูููkeysููุงุญุฏ ูููvaluesุงูู ุฑุชุจุทุฉ ุจููุง. - ุงูู Key Rules: ุงูู
Keyู ูููุนุด ููููnullููุงุฒู ูููู ูุฑูุฏ (unique). - ุงูู Value Rules: ุงูู
Valueู ู ูู ุชูููnullุฃู ู ูุฑุฑุฉ ุนุงุฏู. - ุงูู Key Type: ูุงุฒู
ูู ุงูู
keysูู ููุณ ุงููSortedListุชููู ู ู ููุณ ููุน ุงูุจูุงูุงุช. ูู ุญุงููุช ุชุถููkeyุจููุน ู ุฎุชููุ ูุชุงุฎุฏException. - ุงูู Type Safety: ุจู
ุง ุฅูู
Non-Genericุ ููู ุจููุจู ุฃู ููุน ู ู ุงูุจูุงูุงุช ูู ุงููValuesูุฃูู ุจูุชุนุงู ู ู ุนุงูุง ููobject.
Creating a SortedList
ุฃูู ุญุงุฌุฉุ ูุงุฒู
ูุนู
ู using System.Collections;. ุจุนุฏูู ุจููุดุฆ instance ู
ู ุงูู SortedList.
// Create an empty SortedList
SortedList sortedList = new SortedList();Adding Elements
ุจูุณุชุฎุฏู
Add(key, value) ุนุดุงู ูุถูู ุนูุงุตุฑุ ุฃู ู
ู
ูู ูุณุชุฎุฏู
ุทุฑููุฉ ุงูู collection-initializer.
SortedList sortedList = new SortedList();
// Add elements using the Add() method
sortedList.Add(1, "One");
sortedList.Add(5, "Five");
sortedList.Add(4, "Four");
sortedList.Add(2, "Two");
sortedList.Add(3, "Three");
// Note: Elements will be automatically sorted by key: 1, 2, 3, 4, 5
// --- Important Rules ---
// Duplicate key will throw an ArgumentException
// sortedList.Add(4, "Another Four");
// Null value is allowed
sortedList.Add(6, null);
// Duplicate value is allowed
sortedList.Add(7, "Five");
// Adding a key of a different type (e.g., string) will throw an exception
// sortedList.Add("Ten", "Ten"); Accessing Elements
ุฏู ูุงุญุฏุฉ ู
ู ุฃููู ู
ู
ูุฒุงุช ุงูู SortedListุ ูุฅูู ุชูุฏุฑ ุชูุตู ููุนูุงุตุฑ ุจุฃูุชุฑ ู
ู ุทุฑููุฉ.
Using Keys
ุฒู ุงูู Hashtableุ ุจูุณุชุฎุฏู
ุงูู indexer [] ู
ุน ุงูู key.
Console.WriteLine($"Key: 1, Value: {sortedList[1]}"); // Outputs: OneUsing Index
ุฒู ุงูู ArrayListุ ุจูุณุชุฎุฏู
index (ุงููู ุจูููู ู
ุชุฑุชุจ ุนูู ุญุณุจ ุงูู keys).
// Get value by its sorted index
Console.WriteLine($"Index: 0, Value: {sortedList.GetByIndex(0)}"); // Outputs: One (because key 1 is at index 0)
// Get key by its sorted index
Console.WriteLine($"Index: 0, Key: {sortedList.GetKey(0)}"); // Outputs: 1Using Loops
- ุงูู
foreachloop: ุจููู ุนูู ุงูุนูุงุตุฑ ููDictionaryEntry. - ุงูู
forloop: ู ูุงุณุจ ุฌุฏูุง ููุง ูุฅููุง ููุฏุฑ ูุณุชุฎุฏู ุงููindex.
// Using a foreach loop
Console.WriteLine("\nAccessing with foreach loop:");
foreach (DictionaryEntry item in sortedList)
{
Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
// Using a for loop
Console.WriteLine("\nAccessing with for loop:");
for (int i = 0; i < sortedList.Count; i++)
{
Console.WriteLine($"Key: {sortedList.GetKey(i)}, Value: {sortedList.GetByIndex(i)}");
}Removing Elements
- ุงูู
Remove(key): ุจุชุญุฐู ุงูุนูุตุฑ ุจุงููkeyุจุชุงุนู. - ุงูู
RemoveAt(index): ุจุชุญุฐู ุงูุนูุตุฑ ุจุงููindexุจุชุงุนู (ุจุนุฏ ุงูุชุฑุชูุจ). - ุงูู
Clear(): ุจุชุญุฐู ูู ุงูุนูุงุตุฑ.
// Remove element with key "USA"
sortedList.Remove("USA");
// Remove element at index 1 (after sorting)
sortedList.RemoveAt(1);
// Clear all elements
sortedList.Clear();
Console.WriteLine($"Total elements after Clear(): {sortedList.Count}"); // Outputs: 0Checking for Availability
- ุงูู
Contains(key)ุฃูContainsKey(key): ุจุชุชุฃูุฏ ูู ูููkeyู ุนูู ู ูุฌูุฏ. - ุงูู
ContainsValue(value): ุจุชุชุฃูุฏ ูู ูููvalueู ุนููุฉ ู ูุฌูุฏุฉ.
Console.WriteLine("Does key 'Ind' exist? " + sortedList.ContainsKey("Ind")); // True
Console.WriteLine("Does value 'India' exist? " + sortedList.ContainsValue("India")); // TrueOther Useful Methods and Properties
- ุงูู
Clone(): ุจุชุนู ูshallow copyู ู ุงููSortedList. - ุงูู
CopyTo(array, index): ุจุชูุณุฎ ุนูุงุตุฑ ุงููSortedListููarrayุนุงุฏูุฉ. ู ู ูู ูู ุงู ุชูุณุฎ ุงููKeysุจุณ ุฃู ุงููValuesุจุณ.sortedList.Keys.CopyTo(myKeysArray, 0); sortedList.Values.CopyTo(myValuesArray, 0); - Properties:
- ุงูู
Keys: ุจุชุฑุฌุนcollectionูููุง ูู ุงููkeys. - ุงูู
Values: ุจุชุฑุฌุนcollectionูููุง ูู ุงููvalues. - ุงูู
Count: ุจุชุฑุฌุน ุนุฏุฏ ุงูุนูุงุตุฑ. - ุงูู
Capacity: ุจุชุฑุฌุน ุงูุณุนุฉ ุงูุชุฎุฒูููุฉ ุงูุญุงููุฉ.
- ุงูู
When to Use SortedList?
ุงูู SortedList ุฃุฏุงุฉ ูููุฉุ ููู ูุงุฒู
ูููู
ุฅููุง ุจุชุฏูุน ุชู
ู ุงูุชุฑุชูุจ ุฏู. ู
ุน ูู ุนู
ููุฉ ุฅุถุงูุฉ ุฃู ุญุฐูุ ุงูู collection ุจุชุญุชุงุฌ ุชุนูุฏ ุชุฑุชูุจ ููุณูุง ุนุดุงู ุชูุถู ู
ุชุฑุชุจุฉ. ุฏู ุจูุฎูููุง ู
ูููุฉ ูู ุงูุฃุฏุงุก (expensive) ูู ุจูุชุนุงู
ู ู
ุน ุนุฏุฏ ูุจูุฑ ุฌุฏูุง ู
ู ุงูุนูุงุตุฑ.
ุงูุฎูุงุตุฉ:
- ุงุณุชุฎุฏู
SortedList: ูู ุง ุชููู ุจุชุชุนุงู ู ู ุนcollectionsุตุบูุฑุฉ ูู ุญุชุงุฌูุง ุชูุถู ู ุชุฑุชุจุฉ ุฏุงูู ูุงุ ูู ุญุชุงุฌ ุชูุตู ููุนูุงุตุฑ ุจุงููkeyูุงููindex. - ุชุฌูุจ
SortedList: ูู ุจุชุชุนุงู ู ู ุนcollectionsูุจูุฑุฉ ุฌุฏูุง ูุงูุจูุงูุงุช ุจุชุชุบูุฑ ุจุงุณุชู ุฑุงุฑ. ูู ุงูุญุงูุฉ ุฏูุ ุงูุฃูุถู ุชุณุชุฎุฏูDictionaryุฃูHashSetุ ุฃู ุญุชูListุนุงุฏูุฉ ูุชุฑุชุจูุง ููุช ู ุง ุชุญุชุงุฌ ุจุณ ุจุงุณุชุฎุฏุงูSort().
ูุตูุญุฉ: ุงูู SortedList ูู non-genericุ ูููุฃููุงุฏ ุงูุญุฏูุซุฉ ุงููู ุจุชูุชู
ุจุงูู type-safety ูุงูุฃุฏุงุกุ ูููุถู ุงุณุชุฎุฏุงู
ุงูุจุฏูู ุงูู generic ููู SortedList<TKey, TValue>.