C# SortedDictionary

PHPz
Release: 2024-09-03 15:24:35
Original
817 people have browsed it

The SortedDictionary class in C# is represented as SortedDictionary which is comprised of keys and values collection where key represents the word and value represents the definition and this key and value pairs are sorted based on the key and this SortedDictionary class belongs to System.Collection.Generics namespace and the keys in SortedDictionary are always distinct, immutable and will not be null but the value can be null if the value type is of type, reference and the operations of insertion and retrieval is faster using SortedDictionary class and the retrieval of key value pair in SortedDictionary class is done using KeyValuePair Structure.

Syntax: 

SortedDictionary<TKey, TValue>variable_name = new SortedDictionary<TKey,  TValue>();
Copy after login

Working of SortedDictionary Class in C#

  • ICollection>, IEnumerable, IReadOnlyCollection>, IEnumerable>, ICollection, IDictionary, IDictionary, IEnumerable, IReadOnlyDictionary Interfaces are implemented by SortedDictionary class.
  • The operations of insertion of elements and removing the elements can be faster using SortedDictionary class.
  • The keys must be distinct and there cannot be duplicate keys in SortedDictionary class.
  • The keys are unique and will not be null in SortedDictionary class.
  • If the type of the value is of type reference, the value is allowed to be null.
  • Same type of key and value pairs can be stored using SortedDictionary class.
  • The SortedDictionary in C# is dynamic in nature which means the size of the SortedDictionary increases as per the need.
  • The sorting is done in decreasing order by SortedDictionary class.
  • The total number of key and value pairs that the SortedDictionary class can hold is the capacity of the SortedDictionary class.

Constructors of C# SortedDictionary

Given below are the constructors of C# SortedDictionary:

1. SortedDictionary()

An instance of the SortedDictionary class is initialized which is empty and the implementation of IComparer is used by default for the type, key.

2. SortedDictionary(IComparer)

An instance of the SortedDictionary class is initialized which is empty and the specified implementation of IComparer is used for key comparison.

3. SortedDictionary(IDictionary)

An instance of the SortedDictionary class is initialized which consists of elements taken from the IDictionary that is specified as a parameter and implementation of ICompareris used by default for the type, key.

4. SortedDictionary(IDictionary, IComparer)

An instance of the SortedDictionary class is initialized which consists of elements copied from the IDictionary that is specified as a parameter and the specified implementation of IComparer is used for key comparison.

Methods of C# SortedDictionary

Given below are the methods :

  • Add(TKey, TValue): An element with key and value specified as parameters is added into the SortedDictionary using Add(TKey, TValue) method.
  • Remove(Tkey): An element with key specified as parameter is removed from the SortedDictionary using Remove(TKey) method.
  • ContainsKey(TKey): The ContainsKey(TKey) method is used to determine if the key specified as parameter is present in the SortedDictionary.
  • ContainsValue(TValue): The ContainsKey(TValue) method is used to determine if the value specified as parameter is present in the SortedDictionary.
  • Clear(): The clear() method is used to clear all the objects from the SortedDictionary.
  • CopyTo(KeyValuePair[], Int32): The CopyTo(KeyValuePair[], Int32) method is used to copy the elements of the SortedDictionary to the array of KeyValuePair structures specified as the parameter with the array starting from the index specified in the parameter.
  • Equals(Object): The Equals(Object) method is used to determine if the object specified as the parameter is equal to the current object.
  • GetEnumerator(): The GetEnumerator() method is used to return an enumerator which loops through the SortedDictionary.
  • GetHashCode(): The GetHashCode() method is the hash function by default.
  • GetType(): The GetType() method returns the current instance type.
  • MemberwiseClone(): The MemberwiseClone() method is used to create a shallow copy of the current object.
  • ToString():The ToString() method is used to return a string which represents the current object.
  • TryGetValue(TKey, TValue): The TryGetValue(TKey, TValue) method is used to obtain the value associated with key specified as parameter.

Examples

Given below are the examples mentioned:

Example #1

C# program to demonstrate Add method, Remove method, ContainsKey method, ContainsValue method and TryGetValue method of Sorted Dictionary class.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//a class called program is defined
public class program
{
//main method is called
public static void Main()
{
//a new sorted dictionary is created with key type int and value type string
SortedDictionary<int, string>st = new SortedDictionary<int, string>();
//Add method is used to add objects to the dictionary
st.Add(30,"India");
st.Add(10,"China");
st.Add(20,"Nepal");
st.Remove(10);
Console.WriteLine("If the key 30 is present?{0}", st.ContainsKey(30));
Console.WriteLine("If the key 20 is present? {0}", st.Contains(new KeyValuePair<int, string>(20, "Nepal")));
//new sorted dictionary of both string key and string value types is defined
SortedDictionary<string, string> st1 = new SortedDictionary<string, string>();
st1.Add("Flag","India");
Console.WriteLine("If the value India is present?{0}", st1.ContainsValue("India"));
string rest;
if(st.TryGetValue(30, out rest))
{
Console.WriteLine("The value of the specified key is {0}", rest);
}
else
{
Console.WriteLine("The specified key is not present.");
}
}
}
Copy after login

Output:

C# SortedDictionary

Explanation:

  • In the above program, a class called program is defined. Then the main method is called. Then a new sorted dictionary is created with key type int and value type string. Then Add method is used to add objects to the sorted dictionary. Then Remove method is used to remove objects from the sorted dictionary.
  • Then new sorted dictionary of both string key and string value types is defined. Then contains value method is used to determine if a certain value is present in the sorted dictionary. Then trygetvalue method is used to obtain the value of a specified key.

Example #2

C# program to demonstrate Add method and Clear method of sorted dictionary class.

Code:

using System;
using System.Collections.Generic;
//a class called check is defined
class check
{
// main method is called
public static void Main()
{
// a new sorted dictionary is created with key type string and value type string
SortedDictionary<string, string> tam = new SortedDictionary<string, string>();
// using add method in dictionary to add the objects to the dictionary
tam.Add("R", "Red");
tam.Add("G", "Green");
tam.Add("Y", "Yellow");
// a foreach loop is used to loop around every key in the dictionary and to obtain each key value
foreach(KeyValuePair<string,string>ra in tam)
{
Console.WriteLine("The key and value pairs is SortedDictionary are = {0} and {1}", ra.Key, ra.Value);
}
//using clear method to remove all the objects from sorted dictionary
tam.Clear();
foreach(KeyValuePair<string,string>tr in tam)
{
Console.WriteLine("The key and value pairs is SortedDictionary are = {0} and {1}", tr.Key, tr.Value);
}
}
}
Copy after login

Output:

C# SortedDictionary

Explanation:

  • In the above program, check is the class defined. Then main method is called. Then a new sorted dictionary is created with key type string and value type string. Then we have used add method to add the objects to the sorted dictionary.
  • Then a for each loop is used to loop around every key in the sorted dictionary to obtain each key value. Then clear method is used to clear the console.

The above is the detailed content of C# SortedDictionary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template