O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 15, “Generic Types and Methods,” 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:

int[] nums = new[] { 1, 2, 3 };object[] numsInDisguise = nums;numsInDisguise[1] = "two"; // Ow!

To see why this is a problem and how this issue is avoided for generic types, see Chapter 15. Luckily, arrays aren’t ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required