Can someone explain please why we have different behaviors when we are working with hash tables?
In fact, when the value is a simple type (integer, string…) or an object type, the behavior is different. When we are working with a simple type and we affect the value to a variable and update it; this will not update the hash table. But when we are working with an object type and we affect the value to a variable and update it; this will update the hash table.
It will be easier to understand with an example ^^.
Simple type:
$hash=@{
a=1
b=2
}
$variable = $hash['a']
$variable = 3
Result: $hash
Name Value
---- -----
a 1
b 2
Object type:
$hash=@{
a=New-Object PSObject -Property @{ obj=1 }
b=New-Object PSObject -Property @{ obj=2 }
}
$variable = $hash['a']
$variable.obj = 3
Result: $hash
Name Value
---- -----
a @{obj=3}
b @{obj=2}