我如何像 C# 一样在 powershell 的参数中声明 out

how i can declare out in parameter in powershell like c#

我有 c# 代码,我想在 PowerShell 中找到此代码的替代方案。我找到了类似 [ref]$parameter 的内容,但它不起作用。 我的代码是:

private static bool testfunction(string param1, out string param 2)
{
    param1 = "";
    param2 += "Hello";
    return true;
}

请给我 PowerShell 中的替代代码。

我试试这个:

class tst 
{

    static test([ref]$param)
    {
        $param.Value  = "world "

    }
}

$test = "ddd"
$test

[tst]::test($test)
$test

这是行不通的。

function testfunction {
   param (
       [string]
       $param1,
       [ref]
       $param2
   )

   $param2.value= "World"
   return $true
}

PS C:\> $hello = "Hello"

PS C:\> testfunction "someString" ([ref]$hello)
True

PS C:\> $hello
World

Powershell 支持 ref 参数。请务必在括号中调用 ref 参数(例如 ([ref] $parameter)。还要注意只在 param 块中声明 [ref] 类型。更多详情:

ss64

希望对您有所帮助

更新

您必须使用 ref 关键字调用您的测试方法 -> 使用 [tst]::test([ref]$test) 而不是 `[tst]::test($test)

PS C:\> $test = "ddd"

PS C:\> $test
ddd

PS C:\> [tst]::test([ref]$test)

PS C:\> $test
world 

当你使用 class 时使用 [ref] :

class tst 
{

    static test([ref]$param)
    {
        $param.Value  = "world "

    }
}

$test = "ddd"
$test

[tst]::test([ref]$test)
$test