Dictionary<TKey, TValue> 類C# 中是鍵和值的集合。它是 System.Collections.Generic 命名空間中的通用集合類。這字典<TKey, TValue>泛型類提供從一組鍵到一組值的映射。字典中的每個添加都包含一個值及其關聯的鍵。使用鍵檢索值非常快,接近 O(1),因為 Dictionary 類是作為哈希表實現的。中的每個鍵字典<TKey, TValue>根據字典的相等比較器,它必須是唯一的。
注意:鍵不能為空,但值可以為空(如果值類型為空)T值是一個引用類型。
參數:
- 鑰匙:表示字典中鍵的類型。
- T值:表示字典中值的類型。
由於 Dictionary<TKey, TValue> 是鍵和值的集合,因此元素類型不是鍵的類型或值的類型。相反,元素類型是鍵類型和值類型的 KeyValuePair <TKey, TValue>。
Constructors
構造函數 | 說明 |
---|---|
字典<TKey, TValue>() | 初始化 Dictionary<TKey, TValue> 類的新實例,該實例為空、具有默認初始容量,並對鍵類型使用默認相等比較器。 |
字典<TKey,TValue>(IDictionary<TKey,TValue>) | 初始化 Dictionary<TKey, TValue> 類的新實例,其中包含從指定 IDictionary<TKey, TValue> 複製的元素,並使用鍵類型的默認相等比較器。 |
字典<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) | 初始化 Dictionary<TKey,TValue> 類的新實例,其中包含從指定 IDictionary<TKey,TValue> 複製的元素並使用指定的 IEqualityComparer<T>。 |
字典<TKey,TValue>(IEqualityComparer<TKey>) | 初始化 Dictionary<TKey,TValue> 類的新實例,該實例為空、具有默認初始容量並使用指定的 IEqualityComparer<T>。 |
字典<TKey,TValue>(Int32) | 初始化 Dictionary 類的新實例,該實例為空、具有指定的初始容量並使用鍵類型的默認相等比較器。 |
字典<TKey,TValue>(Int32, IEqualityComparer<TKey>) | 初始化 Dictionary<TKey,TValue> 類的新實例,該實例為空、具有指定的初始容量並使用指定的 IEqualityComparer<T>。 |
字典<TKey,TValue>(SerializationInfo, StreamingContext) | 使用序列化數據初始化 Dictionary<TKey,TValue> 類的新實例。 |
例子:
C#
// C# code to create a Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Create a new dictionary of
// strings, with string keys.
Dictionary<string, string> myDict =
new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");
// 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("\nThe key/value pairs"+
" in myDict are : ");
foreach(KeyValuePair<string, string> kvp in myDict)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}
輸出:
Total key/value pairs in myDict are : 6 The key/value pairs in myDict are : Key = 1, Value = C Key = 2, Value = C++ Key = 3, Value = Java Key = 4, Value = Python Key = 5, Value = C# Key = 6, Value = HTML
Properties
特性 | 說明 |
---|---|
Comparer | 獲取用於確定字典鍵相等性的 IEqualityComparer<TKey, TValue>。 |
Dictionary.Count | 獲取 Dictionary<TKey, TValue> 中包含的鍵/值對的數量。 |
Dictionary.Item[] | 獲取或設置與指定鍵關聯的值。 |
Dictionary.Keys | 獲取包含 Dictionary<TKey, TValue> 中的鍵的集合。 |
Dictionary.Values | 獲取包含 Dictionary<TKey, TValue> 中的值的集合。 |
示例 1:
C#
// C# code to get the keys
// in the Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Create a new dictionary of
// strings, with string keys.
Dictionary<string, string> myDict =
new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs"+
" in myDict are : " + myDict.Count);
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
myDict.Keys;
// The elements of the KeyCollection
// are strongly typed with the type
// that was specified for dictionary keys.
foreach(string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
}
}
}
輸出:
Total key/value pairs in myDict are : 6 Key = 1 Key = 2 Key = 3 Key = 4 Key = 5 Key = 6
示例 2:
C#
// C# code to get the values
// in the Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Create a new dictionary of
// strings, with string keys.
Dictionary<string, string> myDict =
new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs"+
" in myDict are : " + myDict.Count);
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
myDict.Values;
// The elements of the ValueCollection
// are strongly typed with the type
// that was specified for dictionary values.
foreach(string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
}
}
}
輸出:
Total key/value pairs in myDict are : 6 Value = C Value = C++ Value = Java Value = Python Value = C# Value = HTML
Methods
方法 | 說明 |
---|---|
Dictionary.Add() | 將指定的鍵和值添加到字典中。 |
Dictionary.Clear | 從 Dictionary<TKey, TValue> 中刪除所有鍵和值。 |
Dictionary.ContainsKey() | 確定 Dictionary<TKey, TValue> 是否包含指定的鍵。 |
Dictionary.ContainsValue() | 確定 Dictionary<TKey, TValue> 是否包含特定值。 |
Equals(Object) | 確定指定對象是否等於當前對象。 |
GetEnumerator() | 返回一個迭代 Dictionary<TKey, TValue> 的枚舉器。 |
GetHashCode() | 用作默認的哈希函數。 |
GetObjectData(SerializationInfo, StreamingContext) | 實現 ISerialized 接口並返回序列化 Dictionary<TKey,TValue> 實例所需的數據。 |
GetType() | 獲取當前實例的類型。 |
MemberwiseClone() | 創建當前對象的淺拷貝。 |
OnDeserialization(Object) | 實現 ISerialized 接口並在反序列化完成時引發反序列化事件。 |
Dictionary.Remove | 從 Dictionary<TKey, TValue> 中刪除具有指定鍵的值。 |
ToString() | 返回表示當前對象的字符串。 |
TryGetValue(TKey, TValue) | 獲取與指定鍵關聯的值。 |
示例 1:
C#
// C# code to remove all entries
// from Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Create a new dictionary of
// strings, with string keys.
Dictionary<string, string> myDict =
new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs "+
"in myDict are : " + myDict.Count);
myDict.Clear();
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs in "+
"myDict after Clear() operation are : " +
myDict.Count);
}
}
輸出:
Total key/value pairs in myDict are : 6 Total key/value pairs in myDict after Clear() operation are : 0
示例 2:
C#
// C# code to remove the entry with
// the specified key from the Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Create a new dictionary of
// strings, with string keys.
Dictionary<string, string> myDict =
new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs "+
"in myDict are : " + myDict.Count);
// Remove the entry with the
// specified key from the Dictionary
myDict.Remove("Russia");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs in"+
" myDict after Remove() operation are : " +
myDict.Count);
}
}
輸出:
Total key/value pairs in myDict are : 6 Total key/value pairs in myDict after Remove() operation are : 6
參考:
相關用法
- C# Dictionary.Remove()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Dictionary.Clear用法及代碼示例
- C# Dictionary.ContainsKey()用法及代碼示例
- C# Dictionary.ContainsValue()用法及代碼示例
- C# Dictionary.Remove用法及代碼示例
- C# Dictionary.Count用法及代碼示例
- C# Dictionary.Item[]用法及代碼示例
- C# Dictionary.Keys用法及代碼示例
- C# Dictionary.Values用法及代碼示例
- C# Decimal.GetHashCode()用法及代碼示例
- C# Decimal.GetTypeCode()用法及代碼示例
- C# Decimal.Round()用法及代碼示例
- C# DateTimeOffset.GetHashCode()用法及代碼示例
- C# DateTime.ToString()用法及代碼示例
- C# Decimal.ToString()用法及代碼示例
- C# Double.ToString用法及代碼示例
- C# DateTime.GetDateTimeFormats()用法及代碼示例
- C# DateTimeOffset.FromUnixTimeMilliseconds()用法及代碼示例
- C# DateTime.Add()用法及代碼示例
- C# DateTime.AddDays()用法及代碼示例
- C# DateTime.AddHours()用法及代碼示例
- C# DateTime.AddMilliseconds()用法及代碼示例
- C# DateTime.AddMinutes()用法及代碼示例
- C# DateTime.AddMonths()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 C# | Dictionary Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。