首页  >  文章  >  后端开发  >  C# 排序字典

C# 排序字典

PHPz
PHPz原创
2024-09-03 15:24:35829浏览

C#中的SortedDictionary类表示为SortedDictionary它由键和值集合组成,其中键代表单词,值代表定义,并且该键和值对基于键进行排序,并且该 SortedDictionary类属于 System.Collection.Generics 命名空间,SortedDictionary 中的键始终是不同的、不可变的且不会为 null,但如果值类型是类型、引用,则值可以为 null,并且使用 SortedDictionary 插入和检索操作更快类以及 SortedDictionary 类中键值对的检索是使用 KeyValuePair 结构完成的。

语法:

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

C# 中 SortedDictionary 类的工作

  • ICollection>、IEnumerable、IReadOnlyCollection>、IEnumerable>、ICollection、IDictionary、 IDictionary、IEnumerable、IReadOnlyDictionary接口由 SortedDictionary 类实现。
  • 使用 SortedDictionary 类可以更快地插入元素和删除元素。
  • SortedDictionary 类中的键必须不同,并且不能有重复的键。
  • 键是唯一的,在 SortedDictionary 类中不会为空。
  • 如果值的类型是引用类型,则该值允许为空。
  • 可以使用 SortedDictionary 类存储相同类型的键和值对。
  • C# 中的 SortedDictionary 本质上是动态的,这意味着 SortedDictionary 的大小会根据需要而增加。
  • 排序是通过 SortedDictionary 类按降序完成的。
  • SortedDictionary 类可以容纳的键值对总数就是 SortedDictionary 类的容量。

C# SortedDictionary 的构造函数

下面给出的是 C# SortedDictionary 的构造函数:

1. SortedDictionary()

SortedDictionary 类的实例被初始化,该实例为空,并且类型键默认使用 IComparer 的实现。

2. SortedDictionary(IComparer)

SortedDictionary 类的实例被初始化,该实例为空,并使用 IComparer 的指定实现进行键比较。

3. SortedDictionary(IDictionary)

SortedDictionary 类的实例被初始化,其中包含从指定为参数的 IDictionary 中获取的元素以及默认用于类型键的 ICompareris 实现。

4. SortedDictionary(IDictionary, IComparer)

SortedDictionary 类的实例被初始化,该实例由从指定为参数的 IDictionary 复制的元素组成,并且 IComparer 的指定实现用于键比较。

C# SortedDictionary 的方法

以下是方法:

  • 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.");
}
}
}

Output:

C# 排序字典

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);
}
}
}

Output:

C# 排序字典

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.

以上是C# 排序字典的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# SortedSet下一篇:Abstract Class in C#