user4350786
user4350786

Reputation:

Properly Declare a List Collection?

In Powershell V4, how would I properly declare a List<> collection?

I tried declaring it like this

$listCollection = New-Object 'System.Collections.Generic.List<string>'

It didn't work and gave me errors.

Upvotes: 1

Views: 3003

Answers (1)

user2555451
user2555451

Reputation:

In Powershell, you need to use square brackets [...] when specifying the type of the list's items:

PS > $listCollection = New-Object System.Collections.Generic.List[string]  
PS > $listCollection.GetType()

IsPublic IsSerial Name                                     BaseType                 
-------- -------- ----                                     --------                 
True     True     List`1                                   System.Object            


PS >

Note that this is different from C#, which would use angle brackets <...>.

Upvotes: 2

Related Questions