Skip to content

#16.3 SortedList

SortedList or SortedList<TKey, TValue> collection class stores key-value pairs in the ascending order of key by default. It implements IDictionary and ICollection interfaces, as a result, it can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable.

Derived from: IEnumerator -> IEnumberable -> ICollection -> IList -> IDictionary

PropertiesDescription
CountGets the number of elements actually contained in the SortedList
IsFixedSizeGets the value indicating whether the SortedList has a fixed size.
isReadOnlyGets a value indicating whether the SortedList is read-only.
ItemGets or sets the element at the specific index.
KeysGet the list of keys of SortedList.
ValuesGet the list of values in SortedList.
MethodDescription
Add(object key, object value)Add key-value pairs into SortedList.
Remove( object key)Remove the element with the specified key.
RemoveAt(int index)Remove element at the specific index.
Contains( object key)Checks whether a specified key exists in SortedList.
ClearRemoves all the elements in SortedList.
GetKey(int index)Returns a Key stored at a specified index in the internal array.
GetByIndex(int index)Returns the value by index
using System;
using System.Collections;

namespace ArraylistApplication
{   
    public class Program
    {
        static void Main(string[] args)
        {
            SortedList SL = new SortedList(); //SortedList declaration

            SL.Add(1, "One"); //Adding elements to the SortedList object
            SL.Add(3, "Three");
            SL.Add(2, "Two");
            //SL.Add("string value", 4); //This will throw exception: InvalidOperationException
            //For this SL object the first key value pair is of (int, string) datatype so all the
            //consecutive pairs must also follow the same data type

            SortedList SL1 = new SortedList()
            {
                {"Four", 4 },
                {"Five", 5 },
                {"Six", 6 }
            };

            //Access SortedList uisng key value
            int value = (int)SL1["Four"];
            Console.WriteLine("Accessing the value using a key: {0}", value);

            for (int i = 0; i < SL.Count; i++)
            {
                Console.WriteLine("key: {0}   value: {1}",SL.GetKey(i), SL.GetByIndex(i));
            }

            Console.WriteLine("After Removing elements");
            SL.Remove(3);
            foreach (DictionaryEntry item in SL) // instead of tradition item of var type we would need to use DictionaryEntry type
            {
                Console.WriteLine("Key: {0}  value: {1}", item.Key, item.Value);
            }

            SL1.RemoveAt(2);
            foreach (DictionaryEntry item in SL1)
            {
                Console.WriteLine("Key: {0}  value: {1}", item.Key, item.Value);
            }

            Console.ReadKey();
        }
    }
}
Published inC#

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *