PowerShell 中 Dictionary 和 HashTable 有什麼區別?
雖然 Dictionary 有一些優點,但 PowerShell 程式設計師通常更喜歡 Hashtable。請參見以下區別。
a. 與 Hashtable 相比,Hashtable 易於宣告,而 Dictionary 稍微複雜一些。例如,
要建立一個雜湊表,
$hash = @{ 'Country' = 'India' 'Code' = '91' }
要建立字典,
$citydata = New-Object System.Collections.Generic.Dictionary"[String,Int]" $citydata.Add('India',91)
b. Hashtable 位於名為 Collections 的名稱空間中,而 Dictionary 位於名為 System.Collections.Generic 的名稱空間中。Hashtable 是非泛型的,因此可以是不同資料型別的集合,而 Dictionary 屬於泛型類,因此是特定資料型別的集合。
c. Hashtable 和 Dictionary 的 BaseType 均為 Object,但其資料型別不同。Hashtable 有一個“Hashtable”資料型別,Dictionary 有 Dictionary`2 資料型別。
Hashtable
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Hashtable System.Object
Dictionary
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Dictionary`2 System.Object
廣告