Create Strongly Typed Hash Table

by Mar 18, 2013

A hash table can store any data type. If you want more control, you can create a typed dictionary (which behaves pretty much like a hash table):

$ht = @{}
$htTyped = New-Object 'System.Collections.Generic.Dictionary[string,int]'

$ht.ID = 12
$ht.Person = 'weltner'

$htTyped.ID = 12
$htTyped.Person = 'weltner'

$ht is a classic hash table, and you can store anything in it. $htTyped is a typed dictionary. It accepts only strings as key and integers as value. That's why you get an exception when you try to store a name in it.

Twitter This Tip! ReTweet this Tip!