PowerShell pass by reference not working for me

牧云@^-^@ 提交于 2019-11-30 11:38:13
manojlds

Call like this:

swap ([ref]$a) ([ref]$b)

The mistake of using , is described in the Common Gotchas for PowerShell here on Stack Overflow.

Shay Levy

By the way, PowerShell has a special syntax to swap values, and there isn't a need to use $tmp:

$a,$b = $b,$a
Joel B Fant

Firstly, you're calling it wrong. Putting a comma in the call to swap means you're passing an array of them to objects as the first parameter. If you were to correct it...

swap ([ref]$a) ([ref]$b)

...it would then work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!