Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 空哈希集在返回时变为null_Powershell_Powershell 5.0 - Fatal编程技术网

Powershell 空哈希集在返回时变为null

Powershell 空哈希集在返回时变为null,powershell,powershell-5.0,Powershell,Powershell 5.0,从函数返回空的HashSet,会将对象变为null。是什么导致了这种行为?有没有办法解决它?我不想在任何地方都对空的set case进行特殊处理(即,代替一个干净的if($set.Contains(something))现在它必须是if($set-和$set.Contains(something))) 试试这个功能 function GetASet() { New-Object System.Collections.Generic.HashSet[int] }

从函数返回空的
HashSet
,会将对象变为null。是什么导致了这种行为?有没有办法解决它?我不想在任何地方都对空的set case进行特殊处理(即,代替一个干净的
if($set.Contains(something))
现在它必须是
if($set-和$set.Contains(something))


试试这个功能

    function GetASet() {
        New-Object System.Collections.Generic.HashSet[int]
    }
默认情况下,Powershell(尽管不是非常一致)。您需要提示它在函数中显式返回集合:

  • @($someSet)
  • ,$someSet
  • 写入输出-NoEnumerate$someSet

  • 哦,我以为它只是为本机PowerShell阵列这么做的。所以它基本上对所有实现IEnumerable或类似的东西都这样做?写脚本时我并不喜欢这个功能,尽管它在shell中确实很方便
    @($someSet)
    不起作用(奇怪的是,这是我常用的数组解决方案),但是
    ,$someSet
    和漂亮而明确的
    写入输出-NoEnumerate
    起到了作用。@Voo我不确定实现细节,有很多地方可以展开集合(管道、参数、主机输入等)。但它绝对不限于内置阵列。因为我们讨论的是管道输出,所以这些行看起来很相关:。这是针对PS Core的,但我认为PS for Windows也有同样的功能(只是太懒了,无法深入研究反编译器)。
        function GetASet() {
            New-Object System.Collections.Generic.HashSet[int]
        }