.net 防止 ArrayList.Add() 返回索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2149159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:50:58  来源:igfitidea点击:

Prevent ArrayList.Add() from returning the index

.netpowershellarraylist

提问by Milde

is there a way to supress the return value (=Index) of an ArrayList in Powershell (using System.Collections.ArrayList) or should I use another class?

有没有办法在 Powershell 中抑制 ArrayList 的返回值(=Index)(使用 System.Collections.ArrayList)还是应该使用另一个类?

$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0

thanks in advance Milde

提前致谢 米尔德

回答by Keith Hill

You can cast to void to ignore the return value from the Add method:

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

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

Another option is to redirect to $null:

另一种选择是重定向到 $null:

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

回答by Shay Levy

Two more options :)

还有两个选择:)

Pipe to out-null

管道到

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

Assign the result to $null:

将结果分配给 $null:

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