45

Given this powershell code:

$drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]'
$drivers.Add("nitrous","vx")
$drivers.Add("directx","vd")
$drivers.Add("openGL","vo")

Is it possible to initialize this dictionary directly without having to call the Add method. Like .NET allows you to do?

Something like this?

$foo = New-Object 'System.Collections.Generic.Dictionary[String,String]'{{"a","Alley"},{"b" "bat"}}

[not sure what type of syntax this would involve]

dreftymac
  • 31,404
  • 26
  • 119
  • 182
C.J.
  • 15,637
  • 9
  • 61
  • 77

2 Answers2

80

No. The initialization syntax for Dictionary<TKey,TValue> is C# syntax candy. Powershell has its own initializer syntax support for System.Collections.HashTable (@{}):

$drivers = @{"nitrous"="vx"; "directx"="vd"; "openGL"="vo"};

For [probably] nearly all cases it will work just as well as Dictionary<TKey,TValue>. If you really need Dictionary<TKey,TValue> for some reason, you could make a function that takes a HashTable and iterates through the keys and values to add them to a new Dictionary<TKey,TValue>.


The C# initializer syntax isn't exactly "direct" anyway. The compiler generates calls to Add() from it.

Joel B Fant
  • 24,406
  • 4
  • 66
  • 67
  • I don't really need dictionary. A .NET hash table will work just fine. – C.J. Jul 20 '11 at 17:13
  • 2
    It's worth noting that [a HashTable and a Dictionary do have differences](https://stackoverflow.com/questions/301371/why-is-dictionary-preferred-over-hashtable), but that those differences will largely be considered not relevant for common PowerShell scripting. – Bacon Bits Oct 09 '17 at 12:59
  • Strong-type is a good use case for us to use strictly a dict. I'm attempting to convert a property that was a hashtable to a Dict for that reason. – Mike Jun 13 '23 at 14:32
0
$d = [System.Collections.Generic.Dictionary[String,Object]]::new()
#works now.
$d.Add('keyvalue',  @{x=1; y='abc'})

#Evaluate dictionary
$d
<# outputs
Key      Value 
---      ----- 
keyvalue {y, x}
#>

#evalueate contains key:
$d.Keys.Contains('keyvalue')
<# outputs
True
#>

# Evaluate the value using the key
$d['keyvalue']
<# outputs
Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
y                              abc                                                                                                                                                            
x                              1                                                                                                                                                              
#>

#Evaluate the y property of the object
 $d['keyvalue'].y
<# outputs
abc
#>

# Evaluate the x property of the object
$d['keyvalue'].x
<# outputs
1
#>