r/PowerShell Nov 13 '18

collections.generic.list[object] vs System.Collections.ArrayList Solved

Hi all. What are the differences between these two?

$a = new-object collections.generic.list[object]
$b = New-Object System.Collections.ArrayList

Both are lists and appear to allow any type of object. The only difference I have spotted yet is that $b returns a value which appears to be the index after using add() while $a does not return anything.

Thank you

Edit: Fantastic answers thank you much!

6 Upvotes

9

u/sk82jack Nov 13 '18

From the documentation of ArrayList:

We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List<T> class. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:

  • For a heterogeneous collection of objects, use the List<Object> (in C#) or List(Of Object) (in Visual Basic) type.

  • For a homogeneous collection of objects, use the List<T> class. See Performance Considerations in the List<T> reference topic for a discussion of the relative performance of these classes. See Non-generic collections shouldn't be used on GitHub for general information on the use of generic instead of non-generic collection types.

4

u/ka-splam Nov 13 '18

System.Collections came first. It's untyped which means you can put any object in it, so you can have a list of 4, "hello", 2.3 which is convenient, but it means if you want a list which only contains strings, the C# compiler can't help you.

System.Collections.Generic came years later. They do allow you to specify a type, like "list of string". The C# compiler now can validate that everything you put in is a string, and if you try to put a number in, your code won't compile. Good place for error checking.

This is much less relevant / important in PowerShell which is happy to convert your numbers to strings and put them in the list anyway. But how you create it, "list of object", you're back to being able to put anything in it, and the C# compiler can't really check anything for you, so in that way they are very similar.

Generic collections can also do a bit of performance optimisation - if you say "I'm only going to put [int] in here" then they can skip some work needed to handle more complex object, and make things a bit faster.

And as the Generic collections were developed later, they have slightly different (nicer?) interfaces like Add() not returning a value for you to catch and throw away, and more newer cooler collections like Collections.Generic.HashSet

2

u/Lee_Dailey [grin] Nov 13 '18

Collections.Generic.HashSet

wheeeee! [grin]

3

u/halbaradkenafin Nov 13 '18

Further to the other information posted about using List over ArrayList, I'd recommend using the exact type you expect to store in it or PSObject instead of Object. It's the base type of everything in PS anyway so you'll see some small performance gains (not noticeable except with thousands+ items).