ListDictionary是一個專門的集合。它位於 System.Collections.Specialized 命名空間下。該類型表示非通用字典類型。它是用鏈表實現的。此類是小型列表的字典集合 (System.Collections.IDictionary) 的簡單實現。它實現了 IDictionary 方法和屬性,建議與少量元素(少於 10 個)一起使用。
ListDictionary 類的特征:
- ListDictionary 是使用單鏈表的 IDictionary 的簡單實現。
- 如果元素數量為 10 或更少,則它比 Hashtable 更小且更快。
- 如果性能對於大量元素很重要,則不應使用ListDictionary。
- ListDictionary 中的項目沒有任何保證的順序。
- 鑰匙不能空值,但一個值可以。
Constructors
| 構造函數 | 說明 | 
|---|---|
| ListDictionary() | 使用默認比較器創建一個空的ListDictionary。 | 
| ListDictionary(IComparer) | 使用指定的比較器創建一個空的ListDictionary。 | 
例子:
// 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
Properties
| 屬性 | 說明 | 
|---|---|
| Count | 獲取 ListDictionary 中包含的鍵/值對的數量。 | 
| IsFixedSize | 獲取一個值,該值指示 ListDictionary 是否具有固定大小。 | 
| IsReadOnly | 獲取一個值,該值指示ListDictionary是否為隻讀。 | 
| IsSynchronized | 獲取一個值,該值指示 ListDictionary 是否已同步(線程安全)。 | 
| 項目[對象] | 獲取或設置與指定鍵關聯的值。 | 
| Keys | 獲取包含 ListDictionary 中的鍵的 ICollection。 | 
| SyncRoot | 獲取可用於同步對 ListDictionary 的訪問的對象。 | 
| Values | 獲取包含 ListDictionary 中的值的 ICollection。 | 
示例 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
Methods
| 方法 | 說明 | 
|---|---|
| Add(Object, Object) | 將具有指定鍵和值的條目添加到 ListDictionary 中。 | 
| Clear() | 從 ListDictionary 中刪除所有條目。 | 
| Contains(Object) | 確定ListDictionary是否包含特定 key 。 | 
| 複製到(數組,Int32) | 將 ListDictionary 條目複製到指定索引處的一維 Array 實例。 | 
| Equals(Object) | 確定指定對象是否等於當前對象。 | 
| GetEnumerator() | 返回一個迭代 ListDictionary 的 IDictionaryEnumerator。 | 
| GetHashCode() | 用作默認的哈希函數。 | 
| GetType() | 獲取當前實例的類型。 | 
| MemberwiseClone() | 創建當前對象的淺拷貝。 | 
| Remove(Object) | 從 ListDictionary 中刪除具有指定鍵的條目。 | 
| ToString() | 返回表示當前對象的字符串。 | 
示例 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 :
參考:
相關用法
- C# List.TrimExcess用法及代碼示例
- C# List.FindIndex()用法及代碼示例
- C# List BinarySearch()用法及代碼示例
- C# List FindLastIndex()方法用法及代碼示例
- C# List FindLastIndex()函數用法及代碼示例
- C# ListBox用法及代碼示例
- C# List用法及代碼示例
- C# List和Set的區別用法及代碼示例
- C# Linq Aggregate()用法及代碼示例
- C# Linq Concat()用法及代碼示例
- C# Linq Distinct()用法及代碼示例
- C# Linq Intersect()用法及代碼示例
- C# Linq Reverse()用法及代碼示例
- C# Linq ThenBy()用法及代碼示例
- C# Linq ThenByDescending()用法及代碼示例
- C# Linq Union()用法及代碼示例
- C# LinkedList用法及代碼示例
- C# String Clone()用法及代碼示例
- C# String Compare()用法及代碼示例
- C# String CompareOrdinal()用法及代碼示例
- C# String CompareTo()用法及代碼示例
- C# String Concat()用法及代碼示例
- C# String Contains()用法及代碼示例
- C# String Copy()用法及代碼示例
- C# String CopyTo()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 C# | ListDictionary Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
