C# 排序字典

PHPz
發布: 2024-09-03 15:24:35
原創
826 人瀏覽過

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 、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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板