Working with Collection Initializers

Visual Basic 2010 introduces a new language feature known as collection initializers. Basically 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 types Dim listOfIntegers As New List(Of Integer) From {1, 2, 3, 4}

The preceding code produces the same result as the following:

image

You can easily understand how collection initializers enable writing less code that’s ...

Get Visual Basic® 2010 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.