Working with Collection Initializers

The Visual Basic language implements a feature known as collection initializers. This feature works like the object initializers, except that it is specific for instantiating and populating collections inline. To take advantage of collection initializers, you need to use the From reserved keyword enclosing items within brackets, as demonstrated in the following code:

'With primitive typesDim listOfIntegers As New List(Of Integer) From {1, 2, 3, 4}

The preceding code produces the same result as the following:

Dim listOfIntegers As New List(Of Integer)listOfIntegers.Add(1)listOfIntegers.Add(2)listOfIntegers.Add(3)listOfIntegers.Add(4)

You can easily ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.