防止 ArrayList.Add() 返回索引

问题描述 投票:0回答:3

有没有办法抑制 ArrayList 的返回值(=Index) 在 Powershell 中(使用 System.Collections.ArrayList)或者我应该使用 又一堂课?

$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0
.net powershell arraylist
3个回答
107
投票

您可以强制转换为 void 以忽略 Add 方法的返回值:

[void]$myArrayList.Add("test") 

另一个选项是重定向到 $null:

$myArrayList.Add("test") > $null

28
投票

还有两个选择:)

管道到 out-null

$myArrayList.Add("test") | Out-Null  

将结果赋给$null:

$null = $myArrayList.Add("test")

0
投票

我花了一段时间才找到解决方案。人工智能并没有太大帮助。老谷歌在这里提供帮助和指导

© www.soinside.com 2019 - 2024. All rights reserved.