📌  相关文章
📜  C#| ListDictionary类

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

ListDictionary是一个专门的集合。它位于System.Collections.Specialized命名空间下。此类型表示非通用词典类型。它通过链表实现。此类是针对小型列表的字典集合(System.Collections.IDictionary)的简单实现。它实现了IDictionary方法和属性,建议与少量元素(少于10个)一起使用。

ListDictionary类的特征:

  • ListDictionary是使用单链接列表的IDictionary的简单实现。
  • 如果元素的数量为10个或更少,则它比Hashtable更快并且更小。
  • 如果性能对于大量元素很重要,则不应使用ListDictionary。
  • ListDictionary中的项目没有任何保证的顺序。
  • 键不能为null ,但值可以。

建设者

Constructor Description
ListDictionary() Creates an empty ListDictionary using the default comparer.
ListDictionary(IComparer) Creates an empty ListDictionary using the specified comparer.

例子:

// C# code to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
// Driver code
public static void Main()
{
  
    // Creating a ListDictionary named myDict
    ListDictionary myDict = new ListDictionary();
  
    // Adding key/value pairs in myDict
    myDict.Add("Australia", "Canberra");
    myDict.Add("Belgium", "Brussels");
    myDict.Add("Netherlands", "Amsterdam");
    myDict.Add("China", "Beijing");
    myDict.Add("Russia", "Moscow");
    myDict.Add("India", "New Delhi");
  
    // To get count of key/value pairs in myDict
    Console.WriteLine("Total key/value pairs in myDict are : " + myDict.Count);
      
    // Displaying the key/value pairs in myDict
    Console.WriteLine("The key/value pairs in myDict are : ");
  
    foreach(DictionaryEntry de in myDict)
    {
        Console.WriteLine(de.Key + " " + de.Value);
    }
}
}
输出:
Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi

特性

Property Description
Count Gets the number of key/value pairs contained in the ListDictionary.
IsFixedSize Gets a value indicating whether the ListDictionary has a fixed size.
IsReadOnly Gets a value indicating whether the ListDictionary is read-only.
IsSynchronized Gets a value indicating whether the ListDictionary is synchronized (thread safe).
Item[Object] Gets or sets the value associated with the specified key.
Keys Gets an ICollection containing the keys in the ListDictionary.
SyncRoot Gets an object that can be used to synchronize access to the ListDictionary.
Values Gets an ICollection containing the values in the ListDictionary.

范例1:

// C# code to get the number
// of key/value pairs contained
// in the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // Displaying the number of key/value
        // pairs contained in the ListDictionary
        Console.WriteLine(myDict.Count);
    }
}
输出:
6

范例2:

// C# code to check if ListDictionary is read-only
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // Checking if ListDictionary is read-only
        Console.WriteLine(myDict.IsReadOnly);
    }
}
输出:
False

方法

Method Description
Add(Object, Object) Adds an entry with the specified key and value into the ListDictionary.
Clear() Removes all entries from the ListDictionary.
Contains(Object) Determines whether the ListDictionary contains a specific key.
CopyTo(Array, Int32) Copies the ListDictionary entries to a one-dimensional Array instance at the specified index.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an IDictionaryEnumerator that iterates through the ListDictionary.
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(Object) Removes the entry with the specified key from the ListDictionary.
ToString() Returns a string that represents the current object.

范例1:

// C# code to add an entry with
// the specified key and value
// into the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
// Driver code
public static void Main()
{
  
    // Creating a ListDictionary named myDict
    ListDictionary myDict = new ListDictionary();
  
    myDict.Add("Australia", "Canberra");
    myDict.Add("Belgium", "Brussels");
    myDict.Add("Netherlands", "Amsterdam");
    myDict.Add("China", "Beijing");
    myDict.Add("Russia", "Moscow");
    myDict.Add("India", "New Delhi");
  
    // Displaying the total number of elements in myDict
    Console.WriteLine("Total number of elements in myDict are : " 
                                                  + myDict.Count);
  
    // Displaying the elements in ListDictionary myDict
    foreach(DictionaryEntry de in myDict)
    {
        Console.WriteLine(de.Key + " " + de.Value);
    }
}
}
输出:
Total number of elements in myDict are : 6
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi

范例2:

// C# code to remove all entries
// from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
// Driver code
public static void Main()
{
  
    // Creating a ListDictionary named myDict
    ListDictionary myDict = new ListDictionary();
  
    // Adding key/value pairs in myDict
    myDict.Add("I", "first");
    myDict.Add("II", "second");
    myDict.Add("III", "third");
    myDict.Add("IV", "fourth");
    myDict.Add("V", "fifth");
  
    // To get count of key/value pairs in myDict
    Console.WriteLine("Total key/value pairs in myDict are : " 
                                              + myDict.Count);
                                                
    // Displaying the key/value pairs in myDict
  
    Console.WriteLine("The key/value pairs in myDict are : ");
  
    foreach(DictionaryEntry de in myDict)
    {
        Console.WriteLine(de.Key + " " + de.Value);
    }
  
    // Removing all entries from the ListDictionary
    myDict.Clear();
  
    // To get count of key/value pairs in myDict
    Console.WriteLine("Total key/value pairs in myDict are : " 
                                              + myDict.Count);
  
    // Displaying the key/value pairs in myDict
  
    Console.WriteLine("The key/value pairs in myDict are : ");
  
    foreach(DictionaryEntry de in myDict)
    {
        Console.WriteLine(de.Key + " " + de.Value);
    }
}
}
输出:
Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
I first
II second
III third
IV fourth
V fifth
Total key/value pairs in myDict are : 0
The key/value pairs in myDict are :

参考:

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