Covariance

Covariance enables you to assign strongly typed collections (such as List) of derived classes to IEnumerable collections of abstract classes. The code in Listing 20.4 shows how covariance works.

LISTING 20.4 Covariance in Visual Basic 2015

Module Covariance    Sub Main()        'Using collection initializers        Dim stringsCollection As New List(Of String) _                              From {"Understanding ", "covariance ", "in VB 2015"}        'This code is now legal        Dim variance As IEnumerable(Of Object) = stringsCollection        For Each s In variance            Console.WriteLine(s)        Next        Console.ReadLine()    End SubEnd Module

If you examine Listing 20.4, you see that the ...

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.