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]
No. The initialization syntax for
Dictionary<TKey,TValue>is C# syntax candy. Powershell has its own initializer syntax support forSystem.Collections.HashTable(@{}):For [probably] nearly all cases it will work just as well as
Dictionary<TKey,TValue>. If you really needDictionary<TKey,TValue>for some reason, you could make a function that takes aHashTableand iterates through the keys and values to add them to a newDictionary<TKey,TValue>.The C# initializer syntax isn’t exactly “direct” anyway. The compiler generates calls to
Add()from it.