PowerShell不会将空数组作为数组返回

鉴于这是可行的:

[email protected]()
$ar-is[Array]
符合事实的

为什么这样不行

功能测试{
[email protected]()
返回$arr
}
$ar=测试
$ar-is[Array]
错误的

也就是说,为什么不从测试函数返回空数组

您的函数无法工作,因为PowerShell返回所有未捕获的流输出,而不仅仅是return语句的参数。在此过程中,空数组被损坏为$null。但是,您可以通过使用数组构造运算符()在返回时保留数组:

功能测试{
[email protected]()
返回$arr
}

发表评论