6

I am trying to return List< T> from PowerShell function, but get one of:

  1. null - for empty list
  2. System.Int32 - for list with one element
  3. System.Object[] - for list with more elements

Code:

function CreateClrList
{
    $list = New-Object "System.Collections.Generic.List``1[System.Int32]"
    $list.Add(3)
    $list
}

Write-Host (CreateClrList).GetType()
1
  • Better title: converts IEnumerable collections into object arrays. Commented Nov 24, 2009 at 23:00

4 Answers 4

11

Yeah, powershell unrolls all collections. One solution is to return a collection containing the real collection, using the unary comma:

function CreateClrList
{
    $list = New-Object "System.Collections.Generic.List``1[System.Int32]"
    $list.Add(3)
    ,$list
}
Sign up to request clarification or add additional context in comments.

Comments

1

Note that most of the time, you want Powershell to unroll enumerable types so that piped commands execute faster and with earlier user feedback, since commands down the pipe can start processing the first items and give output.

Comments

0

If you need to return a list of ints, use jachymko's solution.

Otherwise, if you do not particularly care whether what you get is a list or array, but just want to iterate through result, you can wrap result in @() while enumerating, e.g.

$fooList = CreateClrList
foreach ($foo in @($fooList))
{
    ...
}

That would cause @($fooList) to be of array type - either an empty array, array with one element or array with multiple elements.

2 Comments

In that case adding items to the internal list would not be used, int values could be output right away. I think the author wanted the list to be returned exactly.
Good point. You're probably right - that is what he said he wanted. However, it may not be the thing he ultimately wants - just providing alternative option.
0

PowerShell 5.0 added support for classes and hence methods where the return type can be specified. To control the type returned, use a class with a static method:

class ListManager {
    static [System.Collections.Generic.List[int]] Create() {
        [System.Collections.Generic.List[int]] $list =
            [System.Collections.Generic.List[int]]::new()

        $list.Add(3)

        return $list
     }
}

[ListManager]::Create().GetType()

Comments

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.