C# supports generic and non-generic SortedList
. It is recommended to use generic SortedList<TKey, TValue>
because it performs faster and less error-prone than the non-generic SortedList
.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | static void Main(string[] args) {//SortedList of int keys, string values SortedList<int, string> numberNames = new SortedList<int, string>(); numberNames.Add(3, "Three"); numberNames.Add(1, "One"); numberNames.Add(2, "Two"); numberNames.Add(4, null); numberNames.Add(10, "Ten"); numberNames.Add(5, "Five"); //The following will throw exceptions //numberNames.Add("Three", 3); //Compile-time error: key must be int type //numberNames.Add(1, "One"); //Run-time exception: duplicate key //numberNames.Add(null, "Five");//Run-time exception: key cannot be null foreach (var kvp in numberNames) { Console.WriteLine("key: {0}, value: {1}", kvp.Key, kvp.Value); } Console.ReadLine(); } |
Output:
Example 2: SortedList.Add() method adds items to the collection. The following code snippet adds several items to the colleciton.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | static void Main(string[] args) { //creation of sortedlist SortedList<int, string> sortedlist = new SortedList<int, string>(); //add the elements in sortedlist sortedlist.Add(1, "Sunday"); sortedlist.Add(2, "Monday"); sortedlist.Add(3, "Tuesday"); sortedlist.Add(4, "Wednesday"); sortedlist.Add(5, "Thusday"); sortedlist.Add(6, "Friday"); sortedlist.Add(7, "Saturday"); //display the elements of the sortedlist Console.WriteLine("The elements in the SortedList are:"); foreach (KeyValuePair<int, string> pair in sortedlist) { Console.WriteLine("{0} => {1}", pair.Key, pair.Value); } Console.ReadLine(); } |
Output:
Example 3: In the following example, we add an element with the number 7, so is the capacity is 8; if we add 9th elements, the capacity will be 16 (double the previous one).
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 | static void Main(string[] args) { //creation of sortedlist SortedList<int, string> sortedlist = new SortedList<int, string>(); //add the elements in sortedlist sortedlist.Add(1, "Sunday"); sortedlist.Add(2, "Monday"); sortedlist.Add(3, "Tuesday"); sortedlist.Add(4, "Wednesday"); sortedlist.Add(5, "Thusday"); sortedlist.Add(6, "Friday"); sortedlist.Add(7, "Saturday"); //display the elements of the sortedlist Console.WriteLine("The elements in the SortedList are:"); foreach (KeyValuePair<int, string> pair in sortedlist) { Console.WriteLine("{0} => {1}", pair.Key, pair.Value); } //Find the capacity Console.WriteLine("The capacity is:" + sortedlist.Capacity); sortedlist.Add(8, "januray"); sortedlist.Add(9, "april"); Console.WriteLine("After adding two more element the capacity is:" + sortedlist.Capacity); Console.ReadLine(); } |
Example 4:
The Count property counts the number of elements in the SortedList or we can say that Count gets the number of key/value pairs contained in the SortedList<TKey, TValue>. For 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 | static void Main(string[] args) { //creation of sortedlist SortedList<int, string> sortedlist = new SortedList<int, string>(); //add the elements in sortedlist sortedlist.Add(1, "Sunday"); sortedlist.Add(2, "Monday"); sortedlist.Add(3, "Tuesday"); sortedlist.Add(4, "Wednesday"); sortedlist.Add(5, "Thusday"); sortedlist.Add(6, "Friday"); sortedlist.Add(7, "Saturday"); //display the elements of the sortedlist Console.WriteLine("The elements in the SortedList are:"); foreach (KeyValuePair<int, string> pair in sortedlist) { Console.WriteLine("{0} => {1}", pair.Key, pair.Value); } Console.WriteLine("The total number of elements in the sortedlist are:" + sortedlist.Count); sortedlist.Add(8, "januray"); sortedlist.Add(9, "april"); Console.WriteLine("After adding two more element the number of element in sortedlist are:" + sortedlist.Count); Console.ReadLine(); } |
Output:
Example 5:
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.Collections.Generic; namespace SortedList_Example { public class Program { public static void Main(string[] args) { SortedList<int,string> studentlist=new SortedList<int,string>(); //Adding some items to sorted list studentlist.Add(1,"Jack"); studentlist.Add(2,"Mark"); studentlist.Add(3,"Neo"); studentlist.Add(4,"Steven"); studentlist.Add(5,"Clark"); //Showing items for(int i=0;i<studentlist.Count;i++) { Console.WriteLine("Keys : "+studentlist.Keys[i]+"\tValues : "+ studentlist.Values[i]); } //Try to Insert Duplicate Keys try { studentlist.Add(5,"Mathew"); } catch(ArgumentException ex) { Console.WriteLine("This key already exist. "+ex.ToString()); } //Change Keys Values studentlist[1]="James Smith"; //Search a Values if(studentlist.ContainsValue("Steven")) { Console.WriteLine("Items Found at Position : "+ studentlist.IndexOfValue("Steven")); } //Traverse using foreach foreach(KeyValuePair<int,string> k in studentlist) { Console.WriteLine("Key : {0} - Value : {1}",k.Key,k.Value); } //Get the length of SortedList int len=studentlist.Count; Console.WriteLine("Length of StudentList is {0}",len.ToString()); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Keys : 1 Values : Jack Keys : 2 Values : Mark Keys : 3 Values : Neo Keys : 4 Values : Steven Keys : 5 Values : Clark This key already exists. System.ArgumentException: An item with the same key has already been added. Key: 5 Parameter name: key at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value) at SortedList_Example.Program.Main(String[] args) in /home/prashant/Documents/SEODocuments/CONTENT/Program/CSharp/GenericSortedList/Program.cs:line 27 Items Found at Position : 3 Key : 1 - Value : James Smith Key : 2 - Value : Mark Key : 3 - Value : Neo Key : 4 - Value : Steven Key : 5 - Value : Clark Length of StudentList is 5 _ |
[…] Item […]