The Null-Conditional Operator and Collections

In Chapter 4, “Data Types and Expressions,” and in Chapter 14, “Generics and Nullable Types,” you learn a little about the new null-conditional operator. Visual Basic 2015 allows you to use this operator against collections, too. You can definitely use the ?. operator to check whether a collection is null before using it, but you can also take advantage of a special syntax that supports indexing. For example, the following code demonstrates how to retrieve the first item from a list of integers only if the collection is not null:

Function GetList() As List(Of Integer)    Return New List(Of Integer) From {1, 2, 3}End FunctionSub NullCheckDemo()    Dim x As List(Of Integer) ...

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.