Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 1.56 KB

returning-a-collection-as-an-object.md

File metadata and controls

35 lines (24 loc) · 1.56 KB

Returning a collection as an object

If you return a collection object from a function you will end returning an enumerator of the collection rather than the collection object itself. Powershell just does that on the return value; there's no way to change that behavior.

This example function returns nothing.

Function Do-Something {
    $msg = New-Object "System.Collections.Generic.List[String]"
    $msg.Add("One thing")
    $msg.Add("And another")

    return , $msg
}

The workaround is to wrap the collection object in another collection, using the so-called unary comma.

Changing the return statement to mix in a unary comma then causes the function to return a List of strings, with a Count property---a proper generic List object.

Function Do-Something {
    $msg = New-Object "System.Collections.Generic.List[String]"
    $msg.Add("One thing")
    $msg.Add("And another")

    return , $msg
}

© 2016 Dave Hein

Creative Commons License
This work by Dave Hein is licensed under a Creative Commons Attribution 4.0 International License.