Advertisement
Constructor | Description |
---|---|
SortedList() | This constructor creates an empty instance of the SortedList class by using the default initial capacity, hash code provider, and comparer. The capacity of an SortedList is the number of elements it can hold, as the new elements are added to the SortedList, its capacity is automatically increased. |
SortedList(int capacity) | This constructor creates a SortedList that is empty and has a specified initial capacity. |
Properties | Description |
---|---|
Capacity | This property is used to get or set the total number of elements a SortedList can hold. |
Count | This property gives total number of elements in a SortedList. |
IsFixedSize | This property determines whether a SortedList has a fixed size or not. |
IsReadOnly | This property determines whether a SortedList is read-only or not. |
IsSynchronzied | This property determines whether a SortedList is thread-safe or not. |
Item[Object key] | This property gets or sets a value associated with the specified key in a SortedList. 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 containing the keys of a SortedList. |
Values | This property gets an ICollection containing the values of a SortedList. |
Methods | Description |
---|---|
int Add(Object key, Object value) | This method adds the specified key-value pair in a SortedList. |
bool Contains(Object key) | This method checks if a SortedList contains a specific key. |
bool ContainsKey(Object key) | This method checks if a SortedList contains a specific key. |
bool ContainsValue(Object key) | This method checks if a SortedList contains a specific value. |
void Clear() | This method removes all of the elements from a SortedList. |
Object GetKey(int index) | This method gets the key at the specified index of a SortedList object. |
Object GetByIndex(int index) | This method gets the value at the specified index of a SortedList object. |
int IndexOfKey(Object key) | This method returns the zero-based index of the specified key in a SortedList object |
int IndexOfValue(Object value) | This method returns the zero-based index of the first occurrence of the specified value in a SortedList object. |
Ilist GetKeyList() | This method gets a list of keys of a SortedList object. |
IList GetValueList() | This method gets a list of values of a SortedList object. |
IDictionaryEnumerator GetEnumerator() | This method returns to enumerate an entire SortedList. |
void Remove(Object key) | This method removes the element with the specified key in a SortedList. |
void RemoveAt(int index) | This method removes the element at the specified index in a SortedList. |
//C# Example of SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//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 c = sl.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The sorted key-value pairs of SortedList:");
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:
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 of SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//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 c = sl.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of SortedList:");
foreach(String str in c)
Console.WriteLine(str + ": " + sl[str]);
//Calling the method Contains() to see if SortedList contains key "Tom"
Console.WriteLine("Does our SortedList contains the key, Tom: "+ sl.Contains("Tom"));
//Calling the method ContainsKey() to see if SortedList contains key "Ana"
Console.WriteLine("Does our SortedList contains the key, Tom: "+ sl.ContainsKey("Ana"));
//Calling the method ContainsKey() to see if SortedList contains key "Jack"
Console.WriteLine("Does our SortedList contains the key, Jack: "+ sl.ContainsKey("Jack"));
//Calling the method ContainsValue() to see if SortedList contains value 4000
Console.WriteLine("Does our SortedList contains the value, 4000: "+ sl.ContainsValue(4000));
//Calling the method ContainsValue() to see if SortedList contains value 10000
Console.WriteLine("Does our SortedList contains the value, 10000: "+ sl.ContainsValue(10000));
}
}
The contents of SortedList:
Ana: 6000
John: 4000
Max: 1000
Rick: 5000
Tom: 2000
Does our SortedList contains the key, Tom: True
Does our SortedList contains the key, Tom: True
Does our SortedList contains the key, Jack: False
Does our SortedList contains the value, 4000: True
Does our SortedList contains the value, 10000: False
//C# Accessing each key-value pair by its index position in a SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//Adding key-value pairs by theAdd() 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");
sl.Add('K', "Kiwi"); //Trying to store a null as a key
//Getting a list of keys
IList keys = sl.GetKeyList();
//Getting a list of values
IList values = sl.GetValueList();
//Printing all the key-value pairs by their index
//Starting with index 0
Console.WriteLine("The key-value pairs of SortedList:");
for(int i=0;i<sl.Count; i++)
Console.WriteLine("At index " + i + " index-> " + keys[i] + ": " + values[i]);
//Getting a key by its index position 4
//where, the first key is stored at zero-index position
Console.WriteLine("The key at index 4: " + sl.GetKey(4));
//Getting a value associated with a specific index position 4
Console.WriteLine("The value at index 4: " + sl.GetByIndex(4));
//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"));
}
}
The key-value pairs of SortedList:
At 0 index-> A: Apple
At 1 index-> B: Banana
At 2 index-> G: Grapes
At 3 index-> K: Kiwi
At 4 index-> M: Mango
At 5 index-> O: Orange
The key at index 4: M
The value at index 4: Mango
The index of key B: 1
The index of value Mango: 4
//C# Example of SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//Adding key-value pairs by theAdd() 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 c = sl.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of SortedList:");
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 of SortedList
Console.WriteLine("The updated contents of SortedList 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 of SortedList
Console.WriteLine("The updated contents of SortedList after removing the element at index 2:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + sl[ch]);
}
}
The contents of SortedList:
A: Apple
B: Banana
G: Grapes
M: Mango
O: Orange
The updated contents of SortedList after removing keys 'A' and 'M' and their values:
B: Banana
G: Grapes
O: Orange
The updated contents of SortedList after removing the element at index 2:
B: Banana
G: Grapes
//C# Key cannot be null in a SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//Adding key-value pairs by theAdd() 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");
sl.Add(null, "no value"); //Trying to store a null as a key
}
}
Unhandled Exception: System.ArgumentNullException: Key cannot be null.
Parameter name: key
at System.Collections.SortedList.Add(Object key, Object value)
at SortedListDemo.Main(String[] ar)
//C# A value can be null in a SortedList
using System;
using System.Collections;
class SortedListDemo
{
public static void Main(String[] ar)
{
//Creating a SortedList
SortedList sl = new SortedList();
//Adding key-value pairs by theAdd() 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");
sl.Add('N', null); //Trying to store a null as a key
//Getting a collection of the keys using property Keys
ICollection c = sl.Keys;
//Printing the key-value pairs by using the collection of keys to obtain their values.
Console.WriteLine("The contents of SortedList:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + sl[ch]);
}
}
The contents of SortedList:
A: Apple
B: Banana
G: Grapes
M: Mango
N:
O: Orange
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement