C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Incorrect declaration for generic AreaComparer

Chapter 3: Parameterized typing with generics: P96, sample declarations

Created: 1/25/2011
Last updated: 1/25/2011
Severity: Typo

On page 96, I offer a workaround to the lack of variance in the AreaComparer example. I suggest that we can make the class generic:

class AreaComparer<T> : IComparer<IShape> where T : IShape

class AreaComparer : AreaComparer<IShape>

This wouldn't actually help - we need to implement IComparer<T> in order to use it to sort a List<Circle> or something similar. It can be fixed with just a small change though:

class AreaComparer<T> : IComparer<T> where T : IShape

class AreaComparer : AreaComparer<IShape>