Advertisement
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 | 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>. |
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>. |
//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"]);
}
}
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
Advertisement
//C# Example ofSortedList<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 contents of SortedList<String, int>:");
foreach(String str in c)
Console.WriteLine(str + ": " + sl[str]);
//Calling the method ContainsKey() to see ifSortedList<TKey, TValue> contains key "Ana"
Console.WriteLine("Does our SortedList<TKey, TValue> contains the key, Tom: "+ sl.ContainsKey("Ana"));
//Calling the method ContainsKey() to see ifSortedList<TKey, TValue> contains key "Jack"
Console.WriteLine("Does our SortedList<TKey, TValue> contains the key, Jack: "+ sl.ContainsKey("Jack"));
//Calling the method ContainsValue() to see ifSortedList<TKey, TValue> contains value 4000
Console.WriteLine("Does our SortedList<TKey, TValue> contains the value, 4000: "+ sl.ContainsValue(4000));
//Calling the method ContainsValue() to see ifSortedList<TKey, TValue> contains value 10000
Console.WriteLine("Does our SortedList<TKey, TValue> contains the value, 10000: "+ sl.ContainsValue(10000));
}
}
The contents of SortedList<String, int>:
Ana: 6000
John: 4000
Max: 1000
Rick: 5000
Tom: 2000
Does our SortedList<TKey, TValue> contains the key, Tom: True
Does our SortedList<TKey, TValue> contains the key, Jack: False
Does our SortedList<TKey, TValue> contains the value, 4000: True
Does our SortedList<TKey, TValue> contains the value, 10000: False
//C# Accessing index position of key and value in a SortedList<TKey, TValue>
using System;
using System.Collections.Generic;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating the SortedList<TKey, TValue> of type, SortedList<char,String>
//Where keys are of type char and values are of type String
SortedList<char, String> sl = new SortedList<char, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
sl.Add('B', "Banana");
sl.Add('M', "Mango");
sl.Add('G', "Grapes");
sl.Add('A', "Apple");
sl.Add('O', "Orange");
//Getting a collection of the keys using property Keys
ICollection<char> c = sl.Keys;
//Using the keys to obtain the values.
Console.WriteLine("The sorted key-value pairs of SortedList<String, Key>:");
foreach(char str in c)
Console.WriteLine(str + ": " + sl[str]);
//Getting the index of the key 'B'(it is char value)
//where, the first key is stored at zero-index position
Console.WriteLine("The index of key B: " + sl.IndexOfKey('B'));
//Getting the index of value "Mango"
Console.WriteLine("The index of value Mango: " + sl.IndexOfValue("Mango"));
//Getting the index of the key 'M'(it is char value)
//where, the first key is stored at zero-index position
Console.WriteLine("The index of key BM: " + sl.IndexOfKey('M'));
//Getting the index of value "Orange"
Console.WriteLine("The index of value Mango: " + sl.IndexOfValue("Orange"));
}
}
The sorted key-value pairs of SortedList<String, Key>:
A: Apple
B: Banana
G: Grapes
M: Mango
O: Orange
The index of key B: 1
The index of value Mango: 3
The index of key BM: 3
The index of value Mango: 4
//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<char,String>
//Where keys are of type char and values are of type String
SortedList<char, String> sl = new SortedList<char, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
sl.Add('B', "Banana");
sl.Add('M', "Mango");
sl.Add('G', "Grapes");
sl.Add('A', "Apple");
sl.Add('O',"Orange");
//Getting a collection of the keys using property Keys
ICollection<char> c = sl.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of SortedList<TKey, TValue>:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + sl[ch]);
//Calling the method Remove() to remove a key 'A' and its associated value
sl.Remove('A');
//Calling the method Remove() to remove a key 'M' and its associated value
sl.Remove('M');
//Printing all elements ofSortedList<TKey, TValue>
Console.WriteLine("The updated contents of SortedList<TKey, TValue> after removing keys 'A' and 'M' and their values:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + sl[ch]);
//Calling the method RemoveAt() to remove the element at index 3
sl.RemoveAt(2);
//Printing all elements ofSortedList<TKey, TValue>
Console.WriteLine("The updated contents of SortedList<TKey, TValue> after removing the element at index 2:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + sl[ch]);
}
}
The contents of SortedList<TKey, TValue>:
A: Apple
B: Banana
G: Grapes
M: Mango
O: Orange
The updated contents of SortedList<TKey, TValue> after removing keys 'A' and 'M' and their values:
B: Banana
G: Grapes
O: Orange
The updated contents of SortedList<TKey, TValue> after removing the element at index 2:
B: Banana
G: Grapes
//C# Key cannot be null in a SortedList<TKey, TValue>
using System;
using System.Collections.Generic;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating the SortedList<TKey, TValue> of type, SortedList<String,String>
//Where keys are of type String and values are of type String
SortedList<String, String> sl = new SortedList<String, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
sl.Add("B", "Banana");
sl.Add("Ma", "Mango");
sl.Add("Gr", "Grapes");
sl.Add("Ap", "Apple");
sl.Add("Or", "Orange");
sl.Add(null, "no value"); //Trying to store a null as a key
}
}
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value)
at SortedListDemo.Main(String[] ar)
//C# A value can be null in a SortedList<TKey, TValue>
using System;
using System.Collections.Generic;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating the SortedList<TKey, TValue> of type, SortedList<String,String>
//Where keys are of type char and values are of type String
SortedList<String, String> sl = new SortedList<String, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
sl.Add("B", "Banana");
sl.Add("Ma", "Mango");
sl.Add("Gr", "Grapes");
sl.Add("Ap", "Apple");
sl.Add("Or", "Orange");
sl.Add("Nu", null); //Trying to store a null as a value
//Getting a collection of the keys using property Keys
ICollection<String> c = sl.Keys;
//Printing the key-value pairs by using the collection of keys to obtain their values.
Console.WriteLine("The contents of SortedList<String, String>:");
foreach(string key in c)
Console.WriteLine(key + ": " + sl[key]);
}
}
The contents of SortedList:
Ap: Apple
B: Banana
Gr: Grapes
Ma: Mango
Nu:
Or: Orange
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement