saftargholi saftargholi - 11 months ago 56
C# Question

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 . I found something like

but it's doesn't work .
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.

i try this :

class tst

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


$test = "ddd"


this is doesn't work.

Answer Source
function testfunction {
   param (

   $param2.value= "World"
   return $true

PS C:\> $hello = "Hello"

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

PS C:\> $hello

Powershell supports ref parameters. Be sure to call the ref parameter in brackets (e.g. ([ref] $parameter). Be also be aware to only declare [ref]as type in the paramblock. Further details:



hope that helps


You've to call your test method with the ref keyword -> use [tst]::test([ref]$test) instead of `[tst]::test($test)

PS C:\> $test = "ddd"

PS C:\> $test

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

PS C:\> $test