空 HashSet 在 return 上变为空
Empty HashSet becomes null on return
从函数返回空值 HashSet
会将对象变为 null。是什么导致了这种行为,有什么办法可以解决它?我不想在任何地方都对空集情况进行特殊处理(即,现在必须是 if ($set -and $set.Contains(something))
,而不是干净整洁的 if ($set.Contains(something))
)。
function GetASet() {
$someSet = New-Object System.Collections.Generic.HashSet[int]
$someSet
}
[System.Collections.Generic.HashSet[int]]$set = GetASet
$set -eq $null # this is true
为你的函数试试这个
function GetASet() {
New-Object System.Collections.Generic.HashSet[int]
}
默认情况下 Powershell unrolls collections(尽管不是很一致)。您需要在函数中明确提示 return 集合:
@($someSet)
,$someSet
Write-Output -NoEnumerate $someSet
从函数返回空值 HashSet
会将对象变为 null。是什么导致了这种行为,有什么办法可以解决它?我不想在任何地方都对空集情况进行特殊处理(即,现在必须是 if ($set -and $set.Contains(something))
,而不是干净整洁的 if ($set.Contains(something))
)。
function GetASet() {
$someSet = New-Object System.Collections.Generic.HashSet[int]
$someSet
}
[System.Collections.Generic.HashSet[int]]$set = GetASet
$set -eq $null # this is true
为你的函数试试这个
function GetASet() {
New-Object System.Collections.Generic.HashSet[int]
}
Powershell unrolls collections(尽管不是很一致)。您需要在函数中明确提示 return 集合:
@($someSet)
,$someSet
Write-Output -NoEnumerate $someSet