08 Aug C# SortedList
The SortedList is a collection class in C# to store key/ value pairs. The key must be unique and cannot be null. It is sorted by keys.
C# has both generic and non-generic SortedList. Since the generic SortedList<TKey, TValue> is faster, it should be preferred. The generic SortedList class is defined in the System.Collections.Generics namespace:
Create a SortedList
To create a SortedList with an int key and string value pair, use <int, string>. Set the type in angular braces in the case of the generic SortedList.
Let us create a SortedList. The result will get sorted by keys automatically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } } } } |
Output
1 2 3 4 5 6 7 |
Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England |
Access SortedList elements
To access a value from a SortedList, use the key value. Set the key of the value you want to access as an indexer. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("Value with key 2 = "+sList[2]); } } } |
Output
1 2 3 4 5 6 7 8 |
Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England Value with key 2 = Australia |
Delete a SortedList element
To delete a specific key/ value pair, set the key as a parameter of the Remove() method. Let us see an example and delete a key/ value par:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); Console.WriteLine("\nSortedList...\n"); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nRemoving key value pair with key 3"); sList.Remove(3); Console.WriteLine("\nUpdated SortedList...\n"); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England Removing key value pair with key 3 Updated SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 4, Value: South Africa Key: 5, Value: England |
Display only the keys
To display only the keys from the key/ value pair of a SortedList, use the keys property. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); Console.WriteLine("\nSortedList..."); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nSortedList Keys..."); for (int i = 0; i < sList.Count; i++) { Console.WriteLine(sList.Keys[i]); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England SortedList Keys... 1 2 3 4 5 |
Display only the values
To display only the values from the key/ value pair of a SortedList, use the Values property. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); Console.WriteLine("\nSortedList..."); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nSortedList Values..."); for (int i = 0; i < sList.Count; i++) { Console.WriteLine(sList.Values[i]); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England SortedList Values... India Australia New Zealand South Africa England |
Check the SortedList for a specific key
The ContainsKey() method allows you to check a SortedList for a key. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); Console.WriteLine("\nSortedList..."); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nKey 5 is present? "+sList.ContainsKey(5)); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England Key 5 is present? True |
Check the SortedList for a specific value
The ContainsValue() method allows you to check a SortedList for a value. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { SortedList<int, string> sList = new SortedList<int, string>(); sList.Add(4,"South Africa"); sList.Add(1,"India"); sList.Add(5,"England"); sList.Add(2,"Australia"); sList.Add(3,"New Zealand"); Console.WriteLine("\nSortedList..."); foreach (KeyValuePair<int, string> i in sList) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nValue Australia is present? "+sList.ContainsValue("Australia")); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
SortedList... Key: 1, Value: India Key: 2, Value: Australia Key: 3, Value: New Zealand Key: 4, Value: South Africa Key: 5, Value: England Value Australia is present? True |
Read More
No Comments