It seems PowerShell hashtable (@{}
) is map of string→string by default. But I wish that my value type is Int32
so that I could do calculation on it.
How could I specify the type information when declaring a hashtable variable?
It seems PowerShell hashtable (@{}
) is map of string→string by default. But I wish that my value type is Int32
so that I could do calculation on it.
How could I specify the type information when declaring a hashtable variable?
An alternative to Hashtable
is the Dictionary
which allows to explicitly specify type of key and value.
In the following, a dictionary with string
key and int
value will be created:
[Collections.Generic.Dictionary[string, int]] $dict = @{}
$dict['a'] = 42 # Ok
$dict['b'] = '42' # Ok (implicit type conversion)
$dict['c'] = 'abc' # Error: Cannot convert value "abc" to type "System.Int32"
Note that a Dictionary
created this way has case-sensitive keys, contrary to a Hashtable
, whose keys are case-insensitive.
$dict['a'] = 42
$dict['A'] = 21 # Creates a 2nd entry!
To make the Dictionary
case-insensitive like a Hashtable
, it must be created differently, by passing a StringComparer
to the Dictionary
constructor:
$dict = [Collections.Generic.Dictionary[string, int]]::new( [StringComparer]::InvariantCultureIgnoreCase )
$dict['a'] = 42
$dict['A'] = 21 # Overwrites the entry whose key is 'a'
Hashtables map keys to values. The type of the keys and values is immaterial.
If you have an integer in a string and want to assign that as an integer, you can simply cast it on assignment: