4

I am trying to assign an array as a value to a dictionary entry as follows but its throwing exception.

$sd = New-Object 'System.Collections.Generic.SortedDictionary[int, string[]]'
$sd[0] = "Data1a", "asda";

Any idea?

1 Answer 1

7

Use cast to [string[]]:

$sd = New-Object 'System.Collections.Generic.SortedDictionary[int, string[]]'
$sd[0] = [string[]]("Data1a", "asda")

Another options is to change the dictionary value type to object[]:

$sd = New-Object 'System.Collections.Generic.SortedDictionary[int, object[]]'
$sd[0] = "Data1a", "asda"
Sign up to request clarification or add additional context in comments.

2 Comments

brilliant!...i tried casting like u mentioned but i was missing the round brackets. Thanks
Works great. I like the 2nd snippet much more. Casting is dirt :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.