The SortedList collection represents Key-Value pairs in the ascending order of the keys by default. The elements can be accessed by both the key and index as required.
The different constructors and their description is given as follows:
Table: Constructors in SortedList in C#
Source: MSDN
Constructors | Description |
---|---|
SortedList() | This constructor initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object. |
SortedList(IComparer) | This constructor initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the specified IComparer interface. |
SortedList(IComparer, Int32) | This constructor initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the specified IComparer interface. |
SortedList(IDictionary) | This constructor initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the IComparable interface implemented by each key. |
SortedList(IDictionary, IComparer) | This constructor initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the specified IComparer interface. |
SortedList(Int32) | This constructor initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object. |
The different properties and their description is given as follows:
Table: Properties in SortedList in C#
Source: MSDN
Properties | Description |
---|---|
Capacity | This property gets or sets the capacity of a SortedList object. |
Count | This property gets the number of elements contained in a SortedList object. |
IsFixedSize | This property gets a value indicating whether a SortedList object has a fixed size. |
IsReadOnly | This property gets a value indicating whether a SortedList object is read-only. |
IsSynchronized | This property gets a value indicating whether access to a SortedList object is synchronized |
Item[Object] | This property gets and sets the value associated with a specific key in a SortedList object. |
Keys | This property gets the keys in a SortedList object. |
SyncRoot | This property gets an object that can be used to synchronize access to a SortedList object. |
Values | This property gets the values in a SortedList object. |
The different methods and their description is given as follows:
Table: Methods in SortedList in C#
Methods | Description |
---|---|
Add(Object, Object) | This method adds an element with the specified key and value to a SortedList object. |
Clear() | This method removes all elements from a SortedList object. |
Clone() | This method creates a shallow copy of a SortedList object. |
Contains(Object) | This method determines whether a SortedList object contains a specific key. |
ContainsKey(Object) | This method determines whether a SortedList object contains a specific key. |
ContainsValue(Object) | This method determines whether a SortedList object contains a specific value. |
CopyTo(Array, Int32) | This method copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array. |
Equals(Object) | This method determines whether the specified object is equal to the current object. |
GetByIndex(Int32) | This method gets the value at the specified index of a SortedList object. |
GetEnumerator() | This method returns an IDictionaryEnumerator object that iterates through a SortedListobject. |
GetHashCode() | This method serves as the default hash function. |
GetKey(Int32) | This method gets the key at the specified index of a SortedList object. |
GetKeyList() | This method gets the keys in a SortedList object. |
GetType() | This method gets the Type of the current instance. |
GetValueList() | This method gets the values in a SortedList object. |
IndexOfKey(Object) | This method returns the zero-based index of the specified key in a SortedList object. |
IndexOfValue(Object) | This method returns the zero-based index of the first occurrence of the specified value in a SortedList object. |
MemberwiseClone() | This method creates a shallow copy of the current Object. |
Remove(Object) | This method removes the element with the specified key from a SortedList object. |
RemoveAt(Int32) | This method removes the element at the specified index of a SortedList object. |
SetByIndex(Int32, Object) | This method replaces the value at a specific index in a SortedList object. |
Synchronized(SortedList) | This method returns a synchronized (thread-safe) wrapper for a SortedList object. |
ToString() | This method returns a string that represents the current object. |
TrimToSize() | This method sets the capacity to the actual number of elements in a SortedList object. |
Some of the SortedList operations are given as follows:
Elements can be added to the SortedList using the Add() method. It adds the specified key and value pairs to the dictionary. The program that demonstrates this is given as follows:
Source Code: Program to add elements in SortedList in C#
using System; using System.Collections.Generic; namespace SortedListDemo { class Example { static void Main(string[] args) { SortedList<int, string> s = new SortedList<int, string>(); s.Add(9,"Harry"); s.Add(2,"Sally"); s.Add(3,"Clarke"); s.Add(1,"James"); s.Add(7,"Emma"); s.Add(6,"Susan"); Console.WriteLine("SortedList elements are as follows:"); foreach (KeyValuePair<int, string> i in s) { Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value); } } } }
The output of the above program is as follows:
SortedList elements are as follows: Key: 1 Value: James Key: 2 Value: Sally Key: 3 Value: Clarke Key: 6 Value: Susan Key: 7 Value: Emma Key: 9 Value: Harry
An element can be deleted from the SortedList using the Remove() method. This method deletes the key, value pair with the specific key that is provided. The program that demonstrates this is given as follows:
Source Code: Program to delete elements from SortedList in C#
using System; using System.Collections.Generic; namespace SortedListDemo { class Example { static void Main(string[] args) { SortedList<int, string> s = new SortedList<int, string>(); s.Add(9,"Harry"); s.Add(2,"Sally"); s.Add(3,"Clarke"); s.Add(1,"James"); s.Add(7,"Emma"); s.Add(6,"Susan"); Console.WriteLine("Original SortedList elements:"); foreach (KeyValuePair<int, string> i in s) { Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value); } s.Remove(3); s.Remove(6); Console.WriteLine("SortedList elements after deletion:"); foreach (KeyValuePair<int, string> i in s) { Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value); } } } }
The output of the above program is as follows:
Original SortedList elements: Key: 1 Value: James Key: 2 Value: Sally Key: 3 Value: Clarke Key: 6 Value: Susan Key: 7 Value: Emma Key: 9 Value: Harry SortedList elements after deletion: Key: 1 Value: James Key: 2 Value: Sally Key: 7 Value: Emma Key: 9 Value: Harry
The ContainsKey() method finds if a key is present in the SortedList or not. If it is present, ContainsKey() returns TRUE and otherwise it returns FALSE. Similarly, ContainsValue() finds if a value is present in the Dictionary or not. If it is present, ContainsValue() returns TRUE and otherwise it returns FALSE.
The program that demonstrates this is given as follows:
Source Code: Program to find an element in SortedList in C#
using System; using System.Collections.Generic; namespace SortedListDemo { class Example { static void Main(string[] args) { SortedList<int, string> s = new SortedList<int, string>(); s.Add(9,"Harry"); s.Add(2,"Sally"); s.Add(3,"Clarke"); s.Add(1,"James"); s.Add(7,"Emma"); s.Add(6,"Susan"); Console.WriteLine("SortedList elements:"); foreach (KeyValuePair<int, string> i in s) { Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value); } Console.WriteLine(); Console.WriteLine("The key 9 is present in SortedList: " + s.ContainsKey(3)); Console.WriteLine("The value Monica is present in SortedList: " + s.ContainsValue("Monica")); } } }
The output of the above program is as follows:
SortedList elements: Key: 1 Value: James Key: 2 Value: Sally Key: 3 Value: Clarke Key: 6 Value: Susan Key: 7 Value: Emma Key: 9 Value: Harry The key 9 is present in SortedList: True The value Monica is present in SortedList: False
Use the IsFixedSize() property in C# to get a value stating whether the SortedList has a fixed size or not
Let us see an example:
Source Code: Program that checks whether the SortedList has a fixed size or not in C#
using System; using System.Collections; namespace SortedListExample { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("L001", "Elyse"); sList.Add("L002", "Meg"); sList.Add("L003", "Sarah"); sList.Add("L004", "Nicole"); sList.Add("L005", "Alyssa"); sList.Add("L006", "Megan"); sList.Add("L007", "Jess"); sList.Add("L008", "Belinda"); Console.WriteLine("Fixed Size? " + sList.IsFixedSize); } } }
The above program gives the following output:
Fixed Size? False
Use the IsFixedSize() property in C# to get a value stating whether the SortedList is read only or not
Let us see an example:
Source Code: Program that checks whether the SortedList is read only or not in C#
using System; using System.Collections; namespace SortedListExample { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("L001", "Elyse"); sList.Add("L002", "Meg"); sList.Add("L003", "Sarah"); sList.Add("L004", "Nicole"); sList.Add("L005", "Alyssa"); sList.Add("L006", "Megan"); sList.Add("L007", "Jess"); sList.Add("L008", "Belinda"); Console.WriteLine("Read Only? " + sList.IsReadOnly); } } }
The above program gives the following output:
Read Only? False
Use the Keys property to get the keys in the SortedList. Let us see how to implement it in C#:
Source Code: Program that gets the keys in the SortedList in C#
using System; using System.Collections; namespace Demo { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("A", "Mic"); sList.Add("B", "Speaker"); sList.Add("C", "Devices"); sList.Add("D", "Screenguards"); sList.Add("E", "Memory Cards"); ICollection res = sList.Keys; foreach (string k in res) { Console.WriteLine(k); } } } }
The above program gives the following output:
A B C D E
Use the Capacity property to get the maximum size of the SortedList:
Source Code: Program that get the maximum size of the SortedList in C#
using System; using System.Collections; namespace SortedListExample { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("AB01", "Dirk"); sList.Add("AB02", "Nolan"); sList.Add("AB03", "Carst"); sList.Add("AB04", "Darren"); sList.Add("AB05", "Daan"); sList.Add("AB06", "Andre"); sList.Add("AB07", "Timm"); sList.Add("AB08", "Logan"); Console.WriteLine("Capacity = " + sList.Capacity); } } }
The above example gives the following output:
Capacity = 16
Use the Count property to count the number of elements in a SortedList. Let us see an example:
Source Code: Program that counts the number of elements in a SortedList in C#
using System; using System.Collections; namespace SortedListExample { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("AB01", "Dirk"); sList.Add("AB02", "Nolan"); sList.Add("AB03", "Carst"); sList.Add("AB04", "Darren"); sList.Add("AB05", "Daan"); sList.Add("AB06", "Andre"); sList.Add("AB07", "Timm"); sList.Add("AB08", "Logan"); Console.WriteLine("Capacity = " + sList.Capacity); Console.WriteLine("Count = " + sList.Count); } } }
The above program displays the following output:
Capacity = 16 Count = 8
The Values property allows a user to display the values of a SortedList. Let us see an example to display only the values:
Source Code: Program that demonstrates the values property in a SortedList in C#
using System; using System.Collections; namespace SortedListExample { class Example { static void Main(string[] args) { SortedList sList = new SortedList(); sList.Add("AB01", "Dirk"); sList.Add("AB02", "Nolan"); sList.Add("AB03", "Carst"); sList.Add("AB04", "Darren"); sList.Add("AB05", "Daan"); sList.Add("AB06", "Andre"); sList.Add("AB07", "Timm"); sList.Add("AB08", "Logan"); Console.WriteLine("Count = " + sList.Count); Console.WriteLine("Here are the values:"); foreach (string res in sList.Values) { Console.WriteLine(res); } } } }
The above program displays the following output:
Count = 8 Here are the values: Dirk Nolan Carst Darren Daan Andre Timm Logan
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *