Hashtable vs array of custom objects regarding iteration

Viewed 7661

I frequently write lists of things and perform enumeration against them to perform some get/set.

I hate enumerating hashtables, since whenever I have to do it, I have to bend my mind backwards to work with hashtable objects.

$hashtablelistofitems = @{}

$hashtablelistofitems.add("i'm a key", "i'm a value")

foreach ($item in $hashtablelistofitems.keys) {
  $item
  $hashtablelistofitems.item($item)
}

Instead, I usually revert to using a single dimensional array of a custom object with two noteproperties.

$array = @()

$listofitems = "" | select key,value

$listofitems.key = "i'm a key"
$listofitems.value = "i'm a value"

$array += $listofitems

foreach ($item in $listofitems) {
  $item.key
  $item.value
}

Why should I be using a hashtable over this method? Simply because it only guarantees a single value per key?

2 Answers
Related