-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Resolution-AnsweredThe question is answered.The question is answered.
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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 arraysError 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.0Visuals
No response
Metadata
Metadata
Assignees
Labels
Resolution-AnsweredThe question is answered.The question is answered.