📜  C#| OrderedDictionary类别

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

OrderedDictionary类表示键或索引可访问的键/值对的集合。它存在于System.Collections.Specialized命名空间中。

OrderedDictionary类的属性:

  • 每个元素都是存储在DictionaryEntry对象中的键/值对。
  • 键不能为null ,但值可以为null。
  • OrderedDictionary的元素不按键排序。
  • 元素可以通过键或索引来访问。

建设者

Constructor Description
OrderedDictionary() Initializes a new instance of the OrderedDictionary class.
OrderedDictionary(IEqualityComparer) Initializes a new instance of the OrderedDictionary class using the specified comparer.
OrderedDictionary(Int32) Initializes a new instance of the OrderedDictionary class using the specified initial capacity.
OrderedDictionary(Int32, IEqualityComparer) Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer.
OrderedDictionary(SerializationInfo, StreamingContext) Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects.

例子:

// C# code to create a OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("1", "ONE");
        myDict.Add("2", "TWO");
        myDict.Add("3", "THREE");
  
        // Displaying the number of key/value
        // pairs in myDict
        Console.WriteLine(myDict.Count);
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
    }
}

输出:

3
1 --> ONE
2 --> TWO
3 --> THREE

特性

Property Description
Count Gets the number of key/values pairs contained in the OrderedDictionary collection.
IsReadOnly Gets a value indicating whether the OrderedDictionary collection is read-only.
Item[Int32] Gets or sets the value at the specified index.
Item[Object] Gets or sets the value with the specified key.
Keys Gets an ICollection object containing the keys in the OrderedDictionary collection.
Values Gets an ICollection object containing the values in the OrderedDictionary collection.

范例1:

// C# code to check if OrderedDictionary
// collection is read-only
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // Checking if OrderedDictionary
        // collection is read-only
        Console.WriteLine(myDict.IsReadOnly);
    }
}

输出:

False

范例2:

// C# code to get the number of
// key/values pairs contained
// in the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // To Get the number of key/values
        // pairs contained in the OrderedDictionary
        Console.WriteLine("Number of key/value pairs are : " 
                                            + myDict.Count);
    }
}

输出:

Number of key/value pairs are : 4

方法

Method Description
Add(Object, Object) Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.
AsReadOnly() Returns a read-only copy of the current OrderedDictionary collection.
Clear() Removes all elements from the OrderedDictionary collection.
Contains(Object) Determines whether the OrderedDictionary collection contains a specific key.
CopyTo(Array, Int32) Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.
GetHashCode() Serves as the default hash function.
GetObjectData(SerializationInfo, StreamingContext) Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection.
GetType() Gets the Type of the current instance.
Insert(Int32, Object, Object) Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.
MemberwiseClone() Creates a shallow copy of the current Object.
OnDeserialization(Object) Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
Remove(Object) Removes the entry with the specified key from the OrderedDictionary collection.
RemoveAt(Int32) Removes the entry at the specified index from the OrderedDictionary collection.
ToString() Returns a string that represents the current object.

范例1:

// C# code to get a read-only
// copy of the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // To Get a read-only copy of
        // the OrderedDictionary
        OrderedDictionary myDict_1 = myDict.AsReadOnly();
  
        // Checking if myDict_1 is read-only
        Console.WriteLine(myDict_1.IsReadOnly);
    }
}

输出:

True

范例2:

// C# code to remove the entry
// with the specified key from
// the OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
  
        // Removing the entry with the specified
        // key from the OrderedDictionary
        myDict.Remove("key2");
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
    }
}

输出:

Number of elements are : 5
key1 -- value1
key2 -- value2
key3 -- value3
key4 -- value4
key5 -- value5
Number of elements are : 4
key1 -- value1
key3 -- value3
key4 -- value4
key5 -- value5

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.ordereddictionary?view=netframework-4.7.2