Annoyances with Generic Types

The mainstream .NET languages, C# and VB, introduced the concept of generic co- and contravariance as part of the .NET 4 release. Even though the CLR has supported those concepts since the introduction of generics in version 2.0, they never were exposed through the various languages. However, with the advent of LINQ in .NET 3.5, the need became much more apparent. Before explaining those concepts, let’s take a look at one of the little annoyances that pop up when using LINQ:

IEnumerable<Apple> apples = ...;var juice = MakeJuice(from apple in apples                      where apple.Color == Color.Red                      select apple);

In the preceding code, the query expression passed ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.