Working with System.Collections.SortedList in C#

In the last post we have seen how to work with BitArray using C#. In this article I will explore SortedList class and the methods provided.

SortedList class is designed to hold the objects in key/value pairs. These can be accessed using key or by index, if known.

Here is class signature as at MSDN.

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class SortedList : IDictionary, ICollection, 
    IEnumerable, ICloneable

Here is explanation of the constructors provided and how to use them

            // Declare SortedList
            SortedList sortedList;

            // constructor which takes Comparer          
            sortedList = new SortedList(new Comparer(CultureInfo.CurrentCulture));

            // constructor which takes Dictionary 
            sortedList = new SortedList(new Dictionary<int, string>());

            // constructor which takes initial capacity
            sortedList = new SortedList(5);

            // constructor which takes Comparer and initial capacity
            sortedList = new SortedList(new Comparer(CultureInfo.CurrentCulture), 5);

            // constructor with Dictionary and Comparer 
            sortedList =
                new SortedList(new Dictionary<int, string>(), new Comparer(CultureInfo.CurrentUICulture));
            
            // Default empty constructor 
            sortedList = new SortedList();

Items could be added in the combination of key/Value Pairs

            // Add Items as key value pairs
            sortedList.Add(1, "one");
            sortedList.Add(2, "two");
            sortedList.Add(3, "three");

Here are rest of the methods and how to use them

           // get size of SortedList
            int size = sortedList.Capacity;

            // convert to string[] Array using Cast<T>
            // and ToArray()
            string[] stringArray =
                sortedList.Cast<string>().ToArray<string>();

            // check if SortedList contains a key
            sortedList.Contains(1);

            // check if SortedList contains a key
            sortedList.ContainsKey(1);

            // check if SortedList contains a value
            sortedList.ContainsValue("one");

            // copy SortedList To  an Array
            sortedList.CopyTo(new string[5], 0);

            // get total Item count in SortedList 
            int totalItems = sortedList.Count;

            // check if SortedList is == another object
            sortedList.Equals(new SortedList());

            // get item at index;
            sortedList.GetByIndex(0);

            // get Enummerator
            IEnumerator e = sortedList.GetEnumerator();
            while (e.MoveNext())
            {
                Object obj = e.Current;
            }

            // get Key at index;
            sortedList.GetKey(0);

            // get Keys as IList
            IList keyList = sortedList.GetKeyList();

            // get Values as IList
            IList valueList = sortedList.GetValueList();

            // get index of an Item by key
            sortedList.IndexOfKey(1);

            // get index of an Item by value
            sortedList.IndexOfValue("one");

            // check if SortedList is of Fixed Size 
            bool IsFixedSize = sortedList.IsFixedSize;

            // check if SortedList is Read Only
            bool IsReadOnly = sortedList.IsReadOnly;

            // get Keys as ICollection
            ICollection keysCollection = sortedList.Keys;

            // get Values as ICollection
            ICollection valuesCollection = sortedList.Values;

            // get items as filtered IEnumerable of Type T
            IEnumerable filteredList = sortedList.OfType<string>();

            // remove an item:
            sortedList.Remove("one");
            
            // remove an item at:
            sortedList.RemoveAt(0);

            // set an item at index:
            sortedList.SetByIndex(5, "one");
Advertisements

One thought on “Working with System.Collections.SortedList in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s