Skip to content

Functions return collections as array regardless of type #26672

@johncwelch

Description

@johncwelch

Prerequisites

Steps to reproduce

If i define a list, an arraylist or an array in a function, when I attempt to return them, they all return as arrays. In the sample code below I set up a generic list of strings, an array list, and an array. No matter which one I return, the returned value is always an Array, not a list or an arraylist. This is, I hope, obviously suboptimal.

function badArrayReturn {
	#test to demonstrate issues with PowerShell returning collection types incorrectly
	$theList = [System.Collections.Generic.List[string]]::New()
	$theArray = @()
	$theArrayList = [System.Collections.ArrayList]::New()

	$theList.Add("item1")|Out-Null
	$theList.Add("item2")|Out-Null

	$theArray += "item3"
	$theArray += "item4"

	$theArrayList.Add("item5")
	$theArrayList.Add("item6")

	$theList.GetType()

	return $theList

}

$returnedList = badArrayReturn

$returnedList.GetType()

Expected behavior

When I return a generic list or an arraylist from a function, I expect the returned value to stay that way, not be coerced to a standard array.

Actual behavior

lists and arraylists are returned as arrays

Error details

Environment data

$PSVersionTable                     

Name                           Value
----                           -----
PSVersion                      7.6.0-preview.6
PSEdition                      Core
GitCommitId                    7.6.0-preview.6
OS                             macOS 26.3.0
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions