📜  C#中的SortedDictionary类

📅  最后修改于: 2021-05-29 17:32:13             🧑  作者: Mango

在C#中,SortedDictionary 类用于表示键/值对的集合。这对是经过排序的形式,并且对密钥进行了排序。此类在System.Collections.Generic命名空间下定义。在SortedDictionary类中,键是不可变的,始终是唯一的,并且不能为null。如果值的类型是引用类型,则允许在值中使用null。 SortedDictionary类为未排序的数据提供最快的插入和删除操作。使用KeyValuePair结构检索SortedDictionary类的键/值对。

建设者

Constructor Description
SortedDictionary() Initializes a new instance of the SortedDictionary class that is empty and uses the default IComparer implementation for the key type.
SortedDictionary(IComparer) Initializes a new instance of the SortedDictionary class that is empty and uses the specified IComparer implementation to compare keys.
SortedDictionary(IDictionary) Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the default IComparer implementation for the key type.
SortedDictionary(IDictionary, IComparer) Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the specified IComparer implementation to compare keys.

例子:

// C# program to illustrate the concept
// of SortedDictionary()
// in SortedDictionary
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create a new SortedDictionary
        // of strings, with int keys.
        SortedDictionary myDr = 
          new SortedDictionary();
  
        // Adding key/value pairs in myDr
        myDr.Add("One", "C");
        myDr.Add("Two", "C++");
        myDr.Add("Three", "C#");
  
        // Display the key/value pairs
        foreach(KeyValuePair pair in myDr)
        {
            Console.WriteLine("Key: {0} and Value: {1}",
                                  pair.Key, pair.Value);
        }
    }
}
输出:
Key: One and Value: C
Key: Three and Value: C#
Key: Two and Value: C++

特性

Property Description
Comparer Gets the IComparer used to order the elements of the SortedDictionary.
Count Gets the number of key/value pairs contained in the SortedDictionary.
Item[TKey] Gets or sets the value associated with the specified key.
Keys Gets a collection containing the keys in the SortedDictionary.
Values Gets a collection containing the values in the SortedDictionary.

例子:

// C# program to illustrate the concept
// of count property in SortedDictionary
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create a new SortedDictionary
        // of strings, with int keys.
        SortedDictionary myDr = 
        new SortedDictionary();
  
        // Adding key/value pairs in myDr
        myDr.Add(1, "Dog");
        myDr.Add(2, "Cat");
        myDr.Add(3, "Birds");
        myDr.Add(4, "Rabbits");
        myDr.Add(5, "Fish");
        myDr.Add(6, "Hamster");
        myDr.Add(7, "Turtle");
  
        // Display the total number of 
        // key/value pairs present in myDr
        Console.WriteLine("Total number of pairs "+
              "present in myDr : {0}", myDr.Count);
    }
}
输出:
Total number of pairs present in myDr : 7

方法

Method Description
Add(TKey, TValue) Adds an element with the specified key and value into the SortedDictionary.
Clear() Removes all elements from the SortedDictionary.
ContainsKey(TKey) Determines whether the SortedDictionary contains an element with the specified key.
ContainsValue(TValue) Determines whether the SortedDictionary contains an element with the specified value.
CopyTo(KeyValuePair[], Int32) Copies the elements of the SortedDictionary to the specified array of KeyValuePair structures, starting at the specified index.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an enumerator that iterates through the SortedDictionary.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
Remove(TKey) Removes the element with the specified key from the SortedDictionary.
ToString() Returns a string that represents the current object.
TryGetValue(TKey, TValue) Gets the value associated with the specified key.

例子:

// C# program to illustrate how
// to add elements in SortedDictionary
using System;
using System.Collections.Generic;
  
class GFG {
  
    static public void Main()
    {
        // Create a new SortedDictionary
        // of strings, with int keys.
        SortedDictionary myDr = 
        new SortedDictionary();
  
        // Adding key/value pairs in myDr
        myDr.Add(1, "Dog");
        myDr.Add(2, "Cat");
        myDr.Add(3, "Birds");
        myDr.Add(4, "Rabbits");
        myDr.Add(5, "Fish");
        myDr.Add(6, "Hamster");
        myDr.Add(7, "Turtle");
  
        // Display the key/value pair of myDr
        Console.WriteLine("Pet animals list:");
        foreach(KeyValuePair pair in myDr)
        {
            Console.WriteLine("Key:{0} and Value: {1}",
                                 pair.Key, pair.Value);
        }
    }
}
输出:
Pet animals list:
Key:1 and Value: Dog
Key:2 and Value: Cat
Key:3 and Value: Birds
Key:4 and Value: Rabbits
Key:5 and Value: Fish
Key:6 and Value: Hamster
Key:7 and Value: Turtle

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.sorteddictionary-2?view=netframework-4.7.2#definition