powershell - How to specify data type for hashtable? -


it seems powershell hashtable (@{}) map of string→string default. wish value type int32 calculation on it.

how specify type information when declaring hashtable variable?

hashtables map keys values. type of keys , values immaterial.

ps c:\> $ht = @{} ps c:\> $ht[1] = 'foo' ps c:\> $ht['2'] = 42 ps c:\> $ht  name                           value ----                           ----- 2                              42 1                              foo  ps c:\> $fmt = "{0} [{1}]`t-> {2} [{3}]" ps c:\> $ht.keys | % {$fmt -f $_, $_.gettype().name, $ht[$_], $ht[$_].gettype().name} 2 [string]      -> 42 [int32] 1 [int32]       -> foo [string]

if have integer in string , want assign integer, can cast on assignment:

ps c:\> $ht[3] = [int]'23' ps c:\> $ht.keys | % {$fmt -f $_, $_.gettype().name, $ht[$_], $ht[$_].gettype().name} 2 [string]      -> 42 [int32] 3 [int32]       -> 23 [int32] 1 [int32]       -> foo [string]

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -