[英]how i can declare out in parameter in powershell like c#
I have c# code and i want to find alternative of this code in PowerShell . 我有c#代码,我想在PowerShell中找到此代码的替代方法。 I found something like
[ref]$parameter
but it's doesn't work . 我发现了
[ref]$parameter
之类的东西,但是它不起作用。 My code is : 我的代码是:
private static bool testfunction(string param1, out string param 2)
{
param1 = "";
param2 += "Hello";
return true;
}
Please give me alternative code in PowerShell. 请在PowerShell中给我其他代码。
i try this : 我尝试这个:
class tst
{
static test([ref]$param)
{
$param.Value = "world "
}
}
$test = "ddd"
$test
[tst]::test($test)
$test
this is doesn't work. 这是行不通的。
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 supports ref parameters. Powershell支持引用参数。 Be sure to call the ref parameter in brackets (eg
([ref] $parameter
). Be also be aware to only declare [ref]
as type in the param
block. Further details: 请确保在方括号中调用ref参数(例如
([ref] $parameter
)。还要注意,仅将[ref]
声明为param
块中的类型)。
hope that helps 希望能有所帮助
UPDATE 更新
You've to call your test method with the ref keyword -> use [tst]::test([ref]$test)
instead of `[tst]::test($test) 您必须使用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
use [ref]
when you use class : 使用class时使用
[ref]
:
class tst
{
static test([ref]$param)
{
$param.Value = "world "
}
}
$test = "ddd"
$test
[tst]::test([ref]$test)
$test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.