ArrayTypeMismatchException

Sometimes it’s not the developer’s fault. Instead, perhaps the runtime or framework designers are to blame. Luckily, this doesn’t happen very often, but ArrayTypeMismatchException shows that it does happen.

The introduction of covariant arrays, as discussed in Chapter 9, “Generics and Collections,” was an unfortunate choice made in the early .NET 1.0 days (for very good reasons back then). This exception catches a mishap in using covariant arrays at runtime that should have instead been caught at compile time:

image

To see why this is a problem and how this issue is avoided for generic types, refer to Chapter 9. Luckily, arrays ...

Get C# 4.0 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.