PowerShell – System.Collections.ArrayList – Add versus AddRange

Background

Recently, I converted a small Powershell code from using Array to ArrayList.

Upon the conversion, the Out-Gridview no longer displayed the actual contents of the collection, but the collection’s metadata.

 

Image

Here is a sample of what is displayed:-

Image -01

Textual -01

  1. Columns
    • Length
    • LongLength
    • Rank
    • IsReadOnly
    • IsFixedSized
    • IsSynchronized
    • Count

 

Sample Code

Code


<#
    Establishes and enforces coding rules in expressions, scripts, and script blocks.
#>
Set-StrictMode -Version latest

#$arrayNumbered = @();
$arrayNumberedAdd = New-Object -TypeName "System.Collections.ArrayList";

$arrayNumberedAddRange = New-Object -TypeName "System.Collections.ArrayList";

$arrayNumbered1To4 = 1..4
$arrayNumbered13To17 = 13..17

#Add Arrays using Add
$null = $arrayNumberedAdd.Add($arrayNumbered1To4 );
$null = $arrayNumberedAdd.Add($arrayNumbered13To17 );

#Add Arrays using AddRange
$null = $arrayNumberedAddRange.AddRange($arrayNumbered1To4 );
$null = $arrayNumberedAddRange.AddRange($arrayNumbered13To17 );
        
$arrayNumbered1To4 | Out-Gridview  -wait
$arrayNumbered13To17 | Out-Gridview  -wait

$arrayNumberedAdd | Out-Gridview  -wait

$arrayNumberedAddRange | Out-Gridview  -wait


Output

Output – $arrayNumber1To4

Output – $arrayNumber13To17

Output – $arrayNumberedAdd

Output – $arrayNumberedAddRange

 

Explanation

  1. Output – $arrayNumber1To4
    • Raw Data
  2. Output – $arrayNumber13To17
    • Raw Data
  3. Output – $arrayNumberAdd
    • Result of adding using Add
  4. Output – $arrayNumberAddRange
    • Result of adding using AddRange

 

Source Code

GitHub

Gist

Powershell – Collection – System.Collections.ArrayList – Add Versus AddRange
Link

Reference

  1. Microsoft
    • Docs > PowerShell > Scripting > Reference > Microsoft.PowerShell.Utility

Summary

If you find that when you employ Out-Grid your data is displayed summarized and not as individual items, please go back and review how you are populating your collections.

That is are you using Add or AddRange?

 

Dedicated

Dedicating this post to those who share incessantly online.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s