Advertisement



< Prev
Next >



C# SortedList<TKey, TValue> Class



The generic SortedList<TKey, TValue> collection class stores a sorted collection of key-value pairs, where: InSortedList<TKey, TValue>, the key-value pairs are sorted by the keys, where each key is used as an index at which its associated value is stored.

SortedList<TKey, TValue> collection class is defined in the System.Collections.Generic namespace and it implements IDictionary, IDictionary<TKey, TValue>, ICollection, IEnumerable<KeyValuePair<TKey, TValue>> and many other interfaces.




Some important features ofSortedList<TKey, TValue>







The commonly used constructors of SortedList<TKey, TValue>


Constructor Description
SortedList<T>()
This constructor creates an empty instance of the SortedList<TKey, TValue> class by using the default initial capacity, hash code provider, and comparer.

The capacity of a SortedList<TKey, TValue> is the number of elements it can hold, as the new elements are added to the SortedList<TKey, TValue>, its capacity is automatically increased.

SortedList<T>(int capacity)
This constructor initializes created anSortedList<TKey, TValue> that is empty and has the specified initial capacity..

SortedList<T>(IDictionary<TKey, TValue>)
This constructor creates an SortedList<T> initialized with the elements of an IDictionary<TKey, TValue> collection.






Properties of SortedList<TKey, TValue>:


Properties Description
Capacity
This property is used to get or set the total number of elements a SortedList<TKey, TValue> can hold.

Count
This property gives total number of elements in a SortedList<TKey, TValue>.

IsFixedSize
This property determines whether a SortedList<TKey, TValue> has a fixed size or not.

IsReadOnly
This property determines whether a SortedList<TKey, TValue> is read-only or not.

IsSynchronzied
This property determines whether a SortedList<TKey, TValue> is thread-safe or not.

Item[Object key]
This property gets or sets a value associated with the specified key in the SortedList<TKey, TValue>.

To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ]

Keys
This property gets an ICollection<TKey> containing the keys of the SortedList<TKey, TValue>.

Values
This property gets an ICollection<TValue containing the values of the SortedList<TKey, TValue>.






Some methods in a SortedList<TKey, TValue>.



Methods Description
void Add(TKey key, TValue value)
This method adds the specified key-value pair in SortedList<TKey, TValue>.

bool ContainsKey(TKey key)
This method checks if SortedList<TKey, TValue> contains a specific key.

bool ContainsValue(TKey key)
This method checks if SortedList<TKey, TValue> contains a specific value.

void Clear()
This method removes all of the elements from the SortedList<TKey, TValue>.

int IndexOfKey(TKey key)
This method returns the zero-based index of the specified key in a SortedList<TKey, TValue> object

int IndexOfValue(TValue value)
This method returns the zero-based index of the first occurrence of the specified value in a SortedList<TKey, TValue> object.

<Generic.KeyValuePair<TKey,TValue>> GetEnumerator()
This method returns to enumerate the entire SortedList<TKey, TValue>.

void Remove(TKey key)
This method removes the element with the specified key in the SortedList<TKey, TValue>.

void RemoveAt(int index)
This method removes the element at the specified index in the SortedList<TKey, TValue>.



Note: Please know that passing an invalid/out-of-range index to any method that takes index position, will throw an ArgumentOutOfRangeException.




SortedList<T> example


We will iterate over all the key-value pairs(sorted on the basis of keys) and we will also obtain a value on the basis its key.

//C# Example of SortedList<TKey, TValue>

using System;
using System.Collections.Generic;

class SortedListDemo
{
public static void Main(String[] ar)
{
	//Creating a SortedList<TKey, TValue> of type, SortedList<String,int>
	//Where keys are of type String and values are of type int
	SortedList<String,int> sl = new SortedList<String, int>();

	//Adding key-value pairs by the Add() method
	sl.Add("Max", 1000);
	sl.Add("John", 4000);
	sl.Add("Tom", 2000);
	sl.Add("Ana", 6000);
	sl.Add("Rick", 5000);

	//Getting a collection of the keys using property Keys
	ICollection<String> c = sl.Keys;
	
	// Use the keys to obtain the values.
	Console.WriteLine("The sorted key-value pairs of SortedList<String, Key>:");
	foreach(String str in c)
		Console.WriteLine(str + ": " + sl[str]);

	//Using the property Item to retrieve the value of key "Tom"
	//Remember the property Item is not used with its name
	Console.WriteLine("Value at the key, Tom is "+ sl["Tom"]);

	//Using the property Item to retrieve the value of key "Ana"
	Console.WriteLine("Value at the key, Ana is "+ sl["Ana"]);
}
}

Output is :

The sorted key-value pairs of SortedList<String, int>:
Ana: 6000
John: 4000
Max: 1000
Rick: 5000
Tom: 2000
Value at the key, Tom is 2000
Value at the key, Ana is 6000


Program Analysis





Advertisement




Checking ifSortedList<TKey, TValue> contains a specific key or a specific value


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement