Selecting elements from a dictionary

Individual elements may be selected by a key. As with the hashtable, two different notations are supported:

$dictionary["Computer1"]    # Key reference 
$dictionary.Computer1       # Dot-notation 

We've seen that, when adding elements, types are converted. Looking back to selecting elements from a hashtable, we know that the value for the key was sensitive to type. As the dictionary has a type declared for the key, it can leverage PowerShell's type conversion.

Consider a dictionary created using a number as a string for the key:

$dictionary = New-Object System.Collections.Generic.Dictionary"[String,IPAddress]" 
$dictionary.Add("1", "192.168.10.222") 
$dictionary.Add("2", "192.168.10.13") 

Each of the following examples works to access the value:

$dictionary."1" 
$dictionary[1] 
$dictionary["1"] 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.244.216