Initialize dictionary at declaration using PowerShell

Viewed 47010

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]

2 Answers

For those who like to really use Dictionary. In my case I had to use another c# dll which has a dictionary as parameter:

New-Object "System.Collections.Generic.Dictionary[String,String]"

This was probably not possible back in 2011

Related