I have a need/want to return a [System.Collections.Generic.List[string]]
from a function, but it is being covered into a System.Object[]
I have this
function TestReturn {
$returnList = New-Object System.Collections.Generic.List[string]
$returnList.Add('Testing, one, two')
return ,@($returnList)
}
$testList = TestReturn
$testList.GetType().FullName
which returns it as a System.Object[]
, and if I change the return line to
return [System.Collections.Generic.List[string]]$returnList
or
return [System.Collections.Generic.List[string]]@($returnList)
it returns a [System.String]
when there is one item in the list, and a System.Object[]
if there is more than one item, in both cases. Is there something odd with a list that it can't be used as a return value?
Now, oddly (I think) it DOES work if I type the variable that receives the value, like this.
[System.Collections.Generic.List[string]]$testList = TestReturn
But that seems like some weird coercion and it doesn't happen with other data types.
System.Array
, but if you do[collections.generic.list[string]]$testList = TestReturn
it should work fine even if the function is returning a string. – Skantze