Prevent ArrayList.Add() from returning the index

.NetPowershellArraylist

.Net Problem Overview


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?

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

.Net Solutions


Solution 1 - .Net

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

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

Another option is to redirect to $null:

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

Solution 2 - .Net

Two more options :)

Pipe to out-null

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

Assign the result to $null:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMildeView Question on Stackoverflow
Solution 1 - .NetKeith HillView Answer on Stackoverflow
Solution 2 - .NetShay LevyView Answer on Stackoverflow