Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new System.Collections.Generic.Dictionary object fails in PowerShell

Tags:

powershell

PowerShell version: 5.x, 6

I'm trying to create a new object of System.Collections.Generic.Dictionary, but it fails.

I tried the following "versions":

> $dictionary = new-object System.Collections.Generic.Dictionary[[string],[int]]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ry = new-object System.Collections.Generic.Dictionary[[string],[int]]
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand

> $dictionary = new-object System.Collections.Generic.Dictionary[string,int]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ionary = new-object System.Collections.Generic.Dictionary[string,int]
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand

I know that I can use a hashtable under PowerShell, but I want to know how to create a dictionary via the above declaration.

What am I missing?

Thx

like image 459
Moerwald Avatar asked Mar 08 '26 15:03

Moerwald


2 Answers

Used type name System.Collections.Generic.Dictionary[[string],[int]] contains a comma. By Creating and initializing an array:

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma

Hence, you need to escape the comma (read the about_Escape_Characters and about_Quoting_Rules help topics). There are more options:

In Windows PowerShell, the escape character is the backtick (`), also called the grave accent (ASCII 96).

$dictionary = new-object System.Collections.Generic.Dictionary[[string]`,[int]]

Quotation marks are used to specify a literal string. You can enclose a string in single quotation marks (') or double quotation marks (").

$dictionary = new-object "System.Collections.Generic.Dictionary[[string],[int]]"

or

$dictionary = new-object 'System.Collections.Generic.Dictionary[[string],[int]]'
like image 168
JosefZ Avatar answered Mar 10 '26 08:03

JosefZ


As well as the accepted answer, a dictionary can also be initialised using the syntax in the code block below, which:

  • removes the need to escape any characters
  • is much cleaner (in my opinion)
  • gives you intellisense (tested in PowerShell & PowerShell ISE)
$dictionary = [System.Collections.Generic.Dictionary[string,int]]::new()

... where string and int are .NET types.

like image 31
TechSpud Avatar answered Mar 10 '26 08:03

TechSpud